Ejemplo n.º 1
0
  /**
   * Is Period Open for Doc Base Type
   *
   * @param DocBaseType document base type
   * @param dateAcct accounting date
   * @return error message or null
   */
  public String isOpen(String DocBaseType, Timestamp dateAcct) {
    if (!isActive()) {
      s_log.warning("Period not active: " + getName());
      return "@C_Period_ID@ <> @IsActive@";
    }

    MAcctSchema as = MClient.get(getCtx(), getAD_Client_ID()).getAcctSchema();
    if (as != null && as.isAutoPeriodControl()) {
      if (!as.isAutoPeriodControlOpen(dateAcct)) return "@PeriodClosed@ - @AutoPeriodControl@";
      //	We are OK
      Timestamp today = new Timestamp(System.currentTimeMillis());
      if (isInPeriod(today) && as.getC_Period_ID() != getC_Period_ID()) {
        as.setC_Period_ID(getC_Period_ID());
        as.save();
      }
      return null;
    }

    //	Standard Period Control
    if (DocBaseType == null) {
      log.warning(getName() + " - No DocBaseType");
      return "@NotFound@ @DocBaseType@";
    }
    MPeriodControl pc = getPeriodControl(DocBaseType);
    if (pc == null) {
      log.warning(getName() + " - Period Control not found for " + DocBaseType);
      return "@NotFound@ @C_PeriodControl_ID@: " + DocBaseType;
    }
    log.fine(getName() + ": " + DocBaseType);
    if (pc.isOpen()) return null;
    return "@PeriodClosed@ - @C_PeriodControl_ID@ (" + DocBaseType + ", " + dateAcct + ")";
  } //	isOpen
Ejemplo n.º 2
0
  /**
   * Execute Task locally and wait
   *
   * @param cmd command
   * @return execution info
   */
  public String executeLocal(String cmd) {
    log.config(cmd);
    if (m_task != null && m_task.isAlive()) m_task.interrupt();

    m_task = new Task(cmd);
    m_task.start();

    StringBuffer sb = new StringBuffer();
    while (true) {
      //  Give it a bit of time
      try {
        Thread.sleep(500);
      } catch (InterruptedException ioe) {
        log.log(Level.SEVERE, cmd, ioe);
      }
      //  Info to user
      sb.append(m_task.getOut())
          .append("\n-----------\n")
          .append(m_task.getErr())
          .append("\n-----------");

      //  Are we done?
      if (!m_task.isAlive()) break;
    }
    log.config("done");
    return sb.toString();
  } //	executeLocal
Ejemplo n.º 3
0
 /**
  * Set Amount (Callout)
  *
  * @param windowNo window
  * @param columnName changed column
  */
 private void setAmt(int windowNo, String columnName) {
   //	get values
   BigDecimal ExpenseAmt = getExpenseAmt();
   int C_Currency_From_ID = getC_Currency_ID();
   int C_Currency_To_ID = getCtx().getContextAsInt("$C_Currency_ID");
   Timestamp DateExpense = getDateExpense();
   //
   log.fine("Amt=" + ExpenseAmt + ", C_Currency_ID=" + C_Currency_From_ID);
   //	Converted Amount = Unit price
   BigDecimal ConvertedAmt = ExpenseAmt;
   //	convert if required
   if (ConvertedAmt.signum() != 0 && C_Currency_To_ID != C_Currency_From_ID) {
     ConvertedAmt =
         MConversionRate.convert(
             getCtx(),
             ConvertedAmt,
             C_Currency_From_ID,
             C_Currency_To_ID,
             DateExpense,
             0,
             getAD_Client_ID(),
             getAD_Org_ID());
   }
   setConvertedAmt(ConvertedAmt);
   log.fine("ConvertedAmt=" + ConvertedAmt);
 } //	setAmt
Ejemplo n.º 4
0
 /**
  * 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;
   ResultSet rs = null;
   try {
     pstmt = DB.prepareStatement(sql, get_Trx());
     pstmt.setInt(1, getPA_Goal_ID());
     rs = pstmt.executeQuery();
     while (rs.next()) list.add(new MGoalRestriction(getCtx(), rs, get_Trx()));
   } catch (Exception e) {
     log.log(Level.SEVERE, sql, e);
   } finally {
     DB.closeStatement(pstmt);
     DB.closeResultSet(rs);
   }
   //
   m_restrictions = new MGoalRestriction[list.size()];
   list.toArray(m_restrictions);
   return m_restrictions;
 } //	getRestrictions
Ejemplo n.º 5
0
  /**
   * Set Resource Assignment - Callout
   *
   * @param oldS_ResourceAssignment_ID old value
   * @param newS_ResourceAssignment_ID new value
   * @param windowNo window
   * @throws Exception
   */
  @UICallout
  public void setS_ResourceAssignment_ID(
      String oldS_ResourceAssignment_ID, String newS_ResourceAssignment_ID, int windowNo)
      throws Exception {
    if (newS_ResourceAssignment_ID == null || newS_ResourceAssignment_ID.length() == 0) return;
    int S_ResourceAssignment_ID = Integer.parseInt(newS_ResourceAssignment_ID);
    if (S_ResourceAssignment_ID == 0) return;
    //
    super.setS_ResourceAssignment_ID(S_ResourceAssignment_ID);

    int M_Product_ID = 0;
    String Name = null;
    String Description = null;
    BigDecimal Qty = null;
    String sql =
        "SELECT p.M_Product_ID, ra.Name, ra.Description, ra.Qty "
            + "FROM S_ResourceAssignment ra"
            + " INNER JOIN M_Product p ON (p.S_Resource_ID=ra.S_Resource_ID) "
            + "WHERE ra.S_ResourceAssignment_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, get_Trx());
      pstmt.setInt(1, S_ResourceAssignment_ID);
      rs = pstmt.executeQuery();
      if (rs.next()) {
        M_Product_ID = rs.getInt(1);
        Name = rs.getString(2);
        Description = rs.getString(3);
        Qty = rs.getBigDecimal(4);
      }
    } catch (SQLException e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    log.fine(
        "S_ResourceAssignment_ID=" + S_ResourceAssignment_ID + " - M_Product_ID=" + M_Product_ID);
    if (M_Product_ID != 0) {
      setM_Product_ID(M_Product_ID);
      if (Description != null) Name += " (" + Description + ")";
      if (!".".equals(Name)) setDescription(Name);
      if (Qty != null) setQty(Qty);
    }
  } //	setS_ResourceAssignment_ID
Ejemplo n.º 6
0
 /**
  * Get Calendar of Period
  *
  * @return calendar
  */
 public int getC_Calendar_ID() {
   if (m_C_Calendar_ID == 0) {
     MYear year = MYear.get(getCtx(), getC_Year_ID());
     if (year != null) m_C_Calendar_ID = year.getC_Calendar_ID();
     else log.severe("@NotFound@ C_Year_ID=" + getC_Year_ID());
   }
   return m_C_Calendar_ID;
 } //	getC_Calendar_ID
Ejemplo n.º 7
0
 /**
  * ************************************************************************ 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_Trx());
       return true;
     }
   }
   return false;
 } //	updateGoal
Ejemplo n.º 8
0
 /**
  * After Save
  *
  * @param newRecord new
  * @param success success
  * @return success
  */
 @Override
 protected boolean afterSave(boolean newRecord, boolean success) {
   if (newRecord) {
     //	SELECT Value FROM AD_Ref_List WHERE AD_Reference_ID=183
     MDocType[] types = MDocType.getOfClient(getCtx());
     int count = 0;
     ArrayList<String> baseTypes = new ArrayList<String>();
     for (MDocType type : types) {
       String DocBaseType = type.getDocBaseType();
       if (baseTypes.contains(DocBaseType)) continue;
       MPeriodControl pc = new MPeriodControl(this, DocBaseType);
       if (pc.save()) count++;
       baseTypes.add(DocBaseType);
     }
     log.fine("PeriodControl #" + count);
   }
   return success;
 } //	afterSave
Ejemplo n.º 9
0
  /**
   * gets all Periods in the Range
   *
   * @param startPeriod
   * @param endPeriod
   * @param calendar_ID
   * @return MPeriod[]
   */
  public static MPeriod[] getAllPeriodsInRange(
      MPeriod startPeriod, MPeriod endPeriod, int calendar_ID, Ctx ctx, Trx trx) {
    if ((startPeriod.getC_Calendar_ID() != calendar_ID)
        || (endPeriod.getC_Calendar_ID() != calendar_ID)) {
      log.saveError("Error", "Periods do not belong to the calendar");
      return null;
    }

    ArrayList<MPeriod> periods = new ArrayList<MPeriod>();
    String sql =
        "SELECT * FROM C_Period WHERE "
            + "C_Period.IsActive='Y' AND PeriodType='S' "
            + "AND C_Period.C_Year_ID IN "
            + "(SELECT C_Year_ID FROM C_Year WHERE C_Year.C_Calendar_ID = ? ) "
            + // calendar_ID
            "AND ((C_Period.C_Year_ID * 1000) + C_Period.PeriodNo) BETWEEN"
            + " (? * 1000 + ?) AND (? * 1000 + ? )"
            + // start Period year ID, Period Number , End Period Year ID, Period Number
            " ORDER BY C_Period.C_Year_ID ASC, C_Period.PeriodNo ASC";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, calendar_ID);
      pstmt.setInt(2, startPeriod.getC_Year_ID());
      pstmt.setInt(3, startPeriod.getPeriodNo());
      pstmt.setInt(4, endPeriod.getC_Year_ID());
      pstmt.setInt(5, endPeriod.getPeriodNo());
      rs = pstmt.executeQuery();
      while (rs.next()) periods.add(new MPeriod(ctx, rs, trx));
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    MPeriod[] retValue = new MPeriod[periods.size()];
    periods.toArray(retValue);
    return retValue;
  }
Ejemplo n.º 10
0
  /**
   * Before Save
   *
   * @param newRecord new
   * @return true
   */
  @Override
  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 (MRole element : roles) {
            if (AD_Role_ID == element.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
Ejemplo n.º 11
0
 /**
  * Get Period Control
  *
  * @param requery requery
  * @return period controls
  */
 public MPeriodControl[] getPeriodControls(boolean requery) {
   if (m_controls != null && !requery) return m_controls;
   //
   ArrayList<MPeriodControl> list = new ArrayList<MPeriodControl>();
   String sql = "SELECT * FROM C_PeriodControl " + "WHERE C_Period_ID=?";
   PreparedStatement pstmt = null;
   ResultSet rs = null;
   try {
     pstmt = DB.prepareStatement(sql, get_Trx());
     pstmt.setInt(1, getC_Period_ID());
     rs = pstmt.executeQuery();
     while (rs.next()) list.add(new MPeriodControl(getCtx(), rs, get_Trx()));
   } catch (Exception e) {
     log.log(Level.SEVERE, sql, e);
   } finally {
     DB.closeResultSet(rs);
     DB.closeStatement(pstmt);
   }
   m_controls = new MPeriodControl[list.size()];
   list.toArray(m_controls);
   return m_controls;
 } //	getPeriodControls
Ejemplo n.º 12
0
/**
 * Location Region Model (Value Object)
 *
 * @author Jorg Janke
 * @version $Id: MRegion.java 8755 2010-05-12 18:30:14Z nnayak $
 */
public final class MRegion extends X_C_Region implements Comparator<PO>, Serializable {
  /** Logger for class MRegion */
  private static final org.compiere.util.CLogger log =
      org.compiere.util.CLogger.getCLogger(MRegion.class);
  /** */
  private static final long serialVersionUID = 1L;

  /**
   * Load Regions (cached)
   *
   * @param ctx context
   */
  private static void loadAllRegions(Ctx ctx) {
    String sql = "SELECT * FROM C_Region WHERE IsActive='Y'";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, (Trx) null);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        MRegion r = new MRegion(ctx, rs, null);
        s_regions.put(String.valueOf(r.getC_Region_ID()), r);
        if (r.isDefault()) s_default = r;
      }
    } catch (SQLException e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }

    s_log.fine(s_regions.size() + " - default=" + s_default);
  } //	loadAllRegions

  /**
   * Get Country (cached)
   *
   * @param ctx context
   * @param C_Region_ID ID
   * @return Country
   */
  public static MRegion get(Ctx ctx, int C_Region_ID) {
    if (s_regions.isEmpty()) loadAllRegions(ctx);
    String key = String.valueOf(C_Region_ID);
    MRegion r = s_regions.get(ctx, key);
    if (r != null) return r;
    r = new MRegion(ctx, C_Region_ID, null);
    if (r.getC_Region_ID() == C_Region_ID) {
      s_regions.put(key, r);
      return r;
    }
    return null;
  } //	get

  /**
   * Get Default Region
   *
   * @param ctx context
   * @return Region or null
   */
  public static MRegion getDefault(Ctx ctx) {
    if (s_regions.isEmpty()) loadAllRegions(ctx);
    return s_default;
  } //	get

  /**
   * Return Regions as Array
   *
   * @param ctx context
   * @return MCountry Array
   */
  public static MRegion[] getRegions(Ctx ctx) {
    if (s_regions.isEmpty()) loadAllRegions(ctx);
    MRegion[] retValue = new MRegion[s_regions.size()];
    s_regions.values().toArray(retValue);
    Arrays.sort(retValue, new MRegion(ctx, 0, null));
    return retValue;
  } //	getRegions

  /**
   * Return Array of Regions of Country
   *
   * @param ctx context
   * @param C_Country_ID country
   * @return MRegion Array
   */
  public static MRegion[] getRegions(Ctx ctx, int C_Country_ID) {
    if (s_regions.isEmpty()) loadAllRegions(ctx);
    ArrayList<MRegion> list = new ArrayList<MRegion>();
    Iterator<MRegion> it = s_regions.values().iterator();
    while (it.hasNext()) {
      MRegion r = it.next();
      if (r.getC_Country_ID() == C_Country_ID) list.add(r);
    }
    //  Sort it
    MRegion[] retValue = new MRegion[list.size()];
    list.toArray(retValue);
    Arrays.sort(retValue, new MRegion(ctx, 0, null));
    return retValue;
  } //	getRegions

  /** Region Cache */
  private static final CCachePerm<String, MRegion> s_regions =
      new CCachePerm<String, MRegion>("C_Region", 100);
  /** Default Region */
  private static MRegion s_default = null;
  /** Static Logger */
  private static CLogger s_log = CLogger.getCLogger(MRegion.class);

  /**
   * ************************************************************************ Create empty Region
   *
   * @param ctx context
   * @param C_Region_ID id
   * @param trx transaction
   */
  public MRegion(Ctx ctx, int C_Region_ID, Trx trx) {
    super(ctx, C_Region_ID, trx);
    if (C_Region_ID == 0) {}
  } //  MRegion

  /**
   * Create Region from current row in ResultSet
   *
   * @param ctx context
   * @param rs result set
   * @param trx transaction
   */
  public MRegion(Ctx ctx, ResultSet rs, Trx trx) {
    super(ctx, rs, trx);
  } //	MRegion

  /**
   * Parent Constructor
   *
   * @param country country
   * @param regionName Region Name
   */
  public MRegion(MCountry country, String regionName) {
    super(country.getCtx(), 0, country.get_Trx());
    setC_Country_ID(country.getC_Country_ID());
    setName(regionName);
  } //  MRegion

  /**
   * Return Name
   *
   * @return Name
   */
  @Override
  public String toString() {
    return getName();
  } //  toString

  /**
   * Compare
   *
   * @param o1 object 1
   * @param o2 object 2
   * @return -1,0, 1
   */
  @Override
  public int compare(PO o1, PO o2) {
    String s1 = o1.toString();
    if (s1 == null) s1 = "";
    String s2 = o2.toString();
    if (s2 == null) s2 = "";
    return s1.compareTo(s2);
  } //	compare

  /**
   * Test / Load
   *
   * @param args
   */
  public static void main(String[] args) {
    Compiere.startup(true);
    /**
     * To add your regions, complete the code below. Please make sure that the file is converted via
     * the Java utility native2ascii - i.e. all seven bit code with /u0000 unicode stuff
     */
    int C_Country_ID = 216; // 	Japan
    MCountry country = new MCountry(Env.getCtx(), C_Country_ID, null);
    // Hokkaido
    MRegion temp = new MRegion(country, "\u5317\u6d77\u9053");
    temp.setDescription("\u5317\u6d77\u9053(Hokkaido)");
    temp.save();
    // Aomori
    temp = new MRegion(country, "\u9752\u68ee\u770c");
    temp.setDescription("\u9752\u68ee\u770c(Aomori)");
    temp.save();
    // Iwate
    temp = new MRegion(country, "\u5ca9\u624b\u770c");
    temp.setDescription("\u5ca9\u624b\u770c(Iwate)");
    temp.save();
    // Miyagi
    temp = new MRegion(country, "\u5bae\u57ce\u770c");
    temp.setDescription("\u5bae\u57ce\u770c(Miyagi)");
    temp.save();
    // Akita
    temp = new MRegion(country, "\u79cb\u7530\u770c");
    temp.setDescription("\u79cb\u7530\u770c(Akita)");
    temp.save();
    // Yamagata
    temp = new MRegion(country, "\u5c71\u5f62\u770c");
    temp.setDescription("\u5c71\u5f62\u770c(Yamagata)");
    temp.save();
    // Fukushima
    temp = new MRegion(country, "\u798f\u5cf6\u770c");
    temp.setDescription("\u798f\u5cf6\u770c(Fukushima)");
    temp.save();
    // Ibaraki
    temp = new MRegion(country, "\u8328\u57ce\u770c");
    temp.setDescription("\u8328\u57ce\u770c(Ibaraki)");
    temp.save();
    // Gunma
    temp = new MRegion(country, "\u7fa4\u99ac\u770c");
    temp.setDescription("\u7fa4\u99ac\u770c(Gunma)");
    temp.save();
    // Saitama
    temp = new MRegion(country, "\u57fc\u7389\u770c");
    temp.setDescription("\u57fc\u7389\u770c(Saitama)");
    temp.save();
    // Chiba
    temp = new MRegion(country, "\u5343\u8449\u770c");
    temp.setDescription("\u5343\u8449\u770c(Chiba)");
    temp.save();
    // Tokyo
    temp = new MRegion(country, "\u6771\u4eac\u90fd");
    temp.setDescription("\u6771\u4eac\u90fd(Tokyo)");
    temp.save();
    // Kanagawa
    temp = new MRegion(country, "\u795e\u5948\u5ddd\u770c");
    temp.setDescription("\u795e\u5948\u5ddd\u770c(Kanagawa)");
    temp.save();
    // Niigata
    temp = new MRegion(country, "\u65b0\u6f5f\u770c");
    temp.setDescription("\u65b0\u6f5f\u770c(Niigata)");
    temp.save();
    // Toyama
    temp = new MRegion(country, "\u5bcc\u5c71\u770c");
    temp.setDescription("\u5bcc\u5c71\u770c(Toyama)");
    temp.save();
    // Ishikawa
    temp = new MRegion(country, "\u77f3\u5ddd\u770c");
    temp.setDescription("\u77f3\u5ddd\u770c(Ishikawa)");
    temp.save();
    // Fukui
    temp = new MRegion(country, "\u798f\u4e95\u770c");
    temp.setDescription("\u798f\u4e95\u770c(Fukui)");
    temp.save();
    // Yamanashi
    temp = new MRegion(country, "\u5c71\u68a8\u770c");
    temp.setDescription("\u5c71\u68a8\u770c(Yamanashi)");
    temp.save();
    // Gifu
    temp = new MRegion(country, "\u5c90\u961c\u770c");
    temp.setDescription("\u5c90\u961c\u770c(Gifu)");
    temp.save();
    // Shizuoka
    temp = new MRegion(country, "\u9759\u5ca1\u770c");
    temp.setDescription("\u9759\u5ca1\u770c(Shizuoka)");
    temp.save();
    // Aichi
    temp = new MRegion(country, "\u611b\u77e5\u770c");
    temp.setDescription("\u611b\u77e5\u770c(Aichi)");
    temp.save();
    // Mie
    temp = new MRegion(country, "\u4e09\u91cd\u770c");
    temp.setDescription("\u4e09\u91cd\u770c(Mie)");
    temp.save();
    // Siga
    temp = new MRegion(country, "\u6ecb\u8cc0\u770c");
    temp.setDescription("\u6ecb\u8cc0\u770c(Siga)");
    temp.save();
    // Kyoto
    temp = new MRegion(country, "\u4eac\u90fd\u5e9c");
    temp.setDescription("\u4eac\u90fd\u5e9c(Kyoto)");
    temp.save();
    // Osaka
    temp = new MRegion(country, "\u5927\u962a\u5e9c");
    temp.setDescription("\u5927\u962a\u5e9c(Osaka)");
    temp.save();
    // Hyogo
    temp = new MRegion(country, "\u5175\u5eab\u770c");
    temp.setDescription("\u5175\u5eab\u770c(Hyogo)");
    temp.save();
    // Nara
    temp = new MRegion(country, "\u5948\u826f\u770c");
    temp.setDescription("\u5948\u826f\u770c(Nara)");
    temp.save();
    // Wakayama
    temp = new MRegion(country, "\u548c\u6b4c\u5c71\u770c");
    temp.setDescription("\u548c\u6b4c\u5c71\u770c(Wakayama)");
    temp.save();
    // Tottori
    temp = new MRegion(country, "\u9ce5\u53d6\u770c");
    temp.setDescription("\u9ce5\u53d6\u770c(Tottori)");
    temp.save();
    // Shimane
    temp = new MRegion(country, "\u5cf6\u6839\u770c");
    temp.setDescription("\u5cf6\u6839\u770c(Shimane)");
    temp.save();
    // Okayama
    temp = new MRegion(country, "\u5ca1\u5c71\u770c");
    temp.setDescription("\u5ca1\u5c71\u770c(Okayama)");
    temp.save();
    // Hiroshima
    temp = new MRegion(country, "\u5e83\u5cf6\u770c");
    temp.setDescription("\u5e83\u5cf6\u770c(Hiroshima)");
    temp.save();
    // Yamaguchi
    temp = new MRegion(country, "\u5c71\u53e3\u770c");
    temp.setDescription("\u5c71\u53e3\u770c(Yamaguchi)");
    temp.save();
    // Tokushima
    temp = new MRegion(country, "\u5fb3\u5cf6\u770c");
    temp.setDescription("\u5fb3\u5cf6\u770c(Tokushima)");
    temp.save();
    // Kagawa
    temp = new MRegion(country, "\u9999\u5ddd\u770c");
    temp.setDescription("\u9999\u5ddd\u770c(Kagawa)");
    temp.save();
    // Ehime
    temp = new MRegion(country, "\u611b\u5a9b\u770c");
    temp.setDescription("\u611b\u5a9b\u770c(Ehime)");
    temp.save();
    // Kouchi
    temp = new MRegion(country, "\u9ad8\u77e5\u770c");
    temp.setDescription("\u9ad8\u77e5\u770c(Kouchi)");
    temp.save();
    // Fukuoka
    temp = new MRegion(country, "\u798f\u5ca1\u770c");
    temp.setDescription("\u798f\u5ca1\u770c(Fukuoka)");
    temp.save();
    // Saga
    temp = new MRegion(country, "\u4f50\u8cc0\u770c");
    temp.setDescription("\u4f50\u8cc0\u770c(Saga)");
    temp.save();
    // Nagasaki
    temp = new MRegion(country, "\u9577\u5d0e\u770c");
    temp.setDescription("\u9577\u5d0e\u770c(Nagasaki)");
    temp.save();
    // Kumamoto
    temp = new MRegion(country, "\u718a\u672c\u770c");
    temp.setDescription("\u718a\u672c\u770c(Kumamoto)");
    temp.save();
    // Ohita
    temp = new MRegion(country, "\u5927\u5206\u770c");
    temp.setDescription("\u5927\u5206\u770c(Ohita)");
    temp.save();
    // Miyasaki
    temp = new MRegion(country, "\u5bae\u5d0e\u770c");
    temp.setDescription("\u5bae\u5d0e\u770c(Miyasaki)");
    temp.save();
    // Kagoshima
    temp = new MRegion(country, "\u9e7f\u5150\u5cf6\u770c");
    temp.setDescription("\u9e7f\u5150\u5cf6\u770c(Kagoshima)");
    temp.save();
    // Okinawa
    temp = new MRegion(country, "\u6c96\u7e04\u770c");
    temp.setDescription("\u6c96\u7e04\u770c(Okinawa)");
    temp.save();
  } //	main
} //	MRegion
Ejemplo n.º 13
0
/**
 * Operating Task Model
 *
 * @author Jorg Janke
 * @version $Id: MTask.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $
 */
public class MTask extends X_AD_Task {
  /** Logger for class MTask */
  private static final org.compiere.util.CLogger log =
      org.compiere.util.CLogger.getCLogger(MTask.class);
  /** */
  private static final long serialVersionUID = 1L;

  /**
   * Standard Constructor
   *
   * @param ctx context
   * @param AD_Task_ID id
   * @param trx p_trx
   */
  public MTask(Ctx ctx, int AD_Task_ID, Trx trx) {
    super(ctx, AD_Task_ID, trx);
  } //	MTask

  /**
   * Load Cosntructor
   *
   * @param ctx ctx
   * @param rs result set
   * @param trx p_trx
   */
  public MTask(Ctx ctx, ResultSet rs, Trx trx) {
    super(ctx, rs, trx);
  } //	MTask

  /** Actual Task */
  private Task m_task = null;

  /**
   * Execute Task and wait
   *
   * @return execution info
   */
  public String execute() {
    String cmd = Msg.parseTranslation(Env.getCtx(), getOS_Command()).trim();
    if (cmd == null || cmd.equals("")) return "Cannot execute '" + getOS_Command() + "'";
    //
    if (isServerProcess()) return executeRemote(cmd);
    return executeLocal(cmd);
  } //	execute

  /**
   * Execute Task locally and wait
   *
   * @param cmd command
   * @return execution info
   */
  public String executeLocal(String cmd) {
    log.config(cmd);
    if (m_task != null && m_task.isAlive()) m_task.interrupt();

    m_task = new Task(cmd);
    m_task.start();

    StringBuffer sb = new StringBuffer();
    while (true) {
      //  Give it a bit of time
      try {
        Thread.sleep(500);
      } catch (InterruptedException ioe) {
        log.log(Level.SEVERE, cmd, ioe);
      }
      //  Info to user
      sb.append(m_task.getOut())
          .append("\n-----------\n")
          .append(m_task.getErr())
          .append("\n-----------");

      //  Are we done?
      if (!m_task.isAlive()) break;
    }
    log.config("done");
    return sb.toString();
  } //	executeLocal

  /**
   * Execute Task locally and wait
   *
   * @param cmd command
   * @return execution info
   */
  public String executeRemote(String cmd) {
    log.config(cmd);
    return "Remote:\n";
  } //	executeRemote

  /**
   * String Representation
   *
   * @return info
   */
  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer("MTask[");
    sb.append(get_ID())
        .append("-")
        .append(getName())
        .append(";Server=")
        .append(isServerProcess())
        .append(";")
        .append(getOS_Command())
        .append("]");
    return sb.toString();
  } //	toString
} //	MTask
Ejemplo n.º 14
0
/**
 * Performance Goal
 *
 * @author Jorg Janke
 * @version $Id: MGoal.java,v 1.2 2006/07/30 00:51:03 jjanke Exp $
 */
public class MGoal extends X_PA_Goal {
  /** Logger for class MGoal */
  private static final org.compiere.util.CLogger log =
      org.compiere.util.CLogger.getCLogger(MGoal.class);
  /** */
  private static final long serialVersionUID = 1L;

  /**
   * Get User Goals
   *
   * @param ctx context
   * @param AD_User_ID user
   * @return array of goals
   */
  public static MGoal[] getUserGoals(Ctx ctx) {
    int AD_Role_ID = ctx.getAD_Role_ID();
    MRole role = MRole.get(ctx, AD_Role_ID);
    int AD_User_ID = ctx.getAD_User_ID();

    if (AD_User_ID < 0) return getTestGoals(ctx);
    ArrayList<MGoal> list = new ArrayList<MGoal>();
    String sql =
        "SELECT * FROM PA_Goal g "
            + "WHERE IsActive='Y'"
            + " AND AD_Client_ID=?" //	#1
            + " AND (";
    if (!role.isWebStoreRole()) sql += " (AD_User_ID IS NULL AND AD_Role_ID IS NULL) OR ";
    sql +=
        " AD_User_ID=?" //	#2
            + " OR EXISTS (SELECT * FROM AD_User_Roles ur "
            + "WHERE ?=ur.AD_User_ID AND g.AD_Role_ID=ur.AD_Role_ID AND ur.IsActive='Y')) "
            + "ORDER BY SeqNo";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, (Trx) null);
      pstmt.setInt(1, ctx.getAD_Client_ID());
      pstmt.setInt(2, AD_User_ID);
      pstmt.setInt(3, AD_User_ID);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        MGoal goal = new MGoal(ctx, rs, null);
        goal.updateGoal(false);
        list.add(goal);
      }
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    MGoal[] retValue = new MGoal[list.size()];
    list.toArray(retValue);
    return retValue;
  } //	getUserGoals

  /**
   * Get Accessible Goals
   *
   * @param ctx context
   * @return array of goals
   */
  public static MGoal[] getGoals(Ctx ctx) {
    ArrayList<MGoal> list = new ArrayList<MGoal>();
    String sql = "SELECT * FROM PA_Goal WHERE IsActive='Y' " + "ORDER BY SeqNo";
    sql =
        MRole.getDefault(ctx, false)
            .addAccessSQL(sql, "PA_Goal", false, true); // 	RW to restrict Access
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, (Trx) null);
      rs = pstmt.executeQuery();
      while (rs.next()) {
        MGoal goal = new MGoal(ctx, rs, null);
        goal.updateGoal(false);
        list.add(goal);
      }
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeStatement(pstmt);
      DB.closeResultSet(rs);
    }
    MGoal[] retValue = new MGoal[list.size()];
    list.toArray(retValue);
    return retValue;
  } //	getGoals

  /**
   * Create Test Goals
   *
   * @param ctx context
   * @return array of goals
   */
  public static MGoal[] getTestGoals(Ctx ctx) {
    MGoal[] retValue = new MGoal[4];
    retValue[0] = new MGoal(ctx, "Test 1", "Description 1", new BigDecimal(1000), null);
    retValue[0].setMeasureActual(new BigDecimal(200));
    retValue[1] = new MGoal(ctx, "Test 2", "Description 2", new BigDecimal(1000), null);
    retValue[1].setMeasureActual(new BigDecimal(900));
    retValue[2] = new MGoal(ctx, "Test 3", "Description 3", new BigDecimal(1000), null);
    retValue[2].setMeasureActual(new BigDecimal(1200));
    retValue[3] = new MGoal(ctx, "Test 4", "Description 4", new BigDecimal(1000), null);
    retValue[3].setMeasureActual(new BigDecimal(3200));
    return retValue;
  } //	getTestGoals

  /**
   * Get Goals with Measure
   *
   * @param ctx context
   * @param PA_Measure_ID measure
   * @return goals
   */
  public static MGoal[] getMeasureGoals(Ctx ctx, int PA_Measure_ID) {
    ArrayList<MGoal> list = new ArrayList<MGoal>();
    String sql = "SELECT * FROM PA_Goal WHERE IsActive='Y' AND PA_Measure_ID=? " + "ORDER BY SeqNo";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, (Trx) null);
      pstmt.setInt(1, PA_Measure_ID);
      rs = pstmt.executeQuery();
      while (rs.next()) list.add(new MGoal(ctx, rs, null));
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeStatement(pstmt);
      DB.closeResultSet(rs);
    }
    MGoal[] retValue = new MGoal[list.size()];
    list.toArray(retValue);
    return retValue;
  } //	getMeasureGoals

  /**
   * Get Multiplier from Scope to Display
   *
   * @param goal goal
   * @return null if error or multiplier
   */
  public static BigDecimal getMultiplier(MGoal goal) {
    String MeasureScope = goal.getMeasureScope();
    String MeasureDisplay = goal.getMeasureDisplay();
    if (MeasureDisplay == null || MeasureScope.equals(MeasureDisplay)) return Env.ONE; // 	1:1

    if (MeasureScope.equals(MEASURESCOPE_Total) || MeasureDisplay.equals(MEASUREDISPLAY_Total))
      return null; //	Error

    BigDecimal Multiplier = null;
    if (MeasureScope.equals(MEASURESCOPE_Year)) {
      if (MeasureDisplay.equals(MEASUREDISPLAY_Quarter)) Multiplier = new BigDecimal(1.0 / 4.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Month)) Multiplier = new BigDecimal(1.0 / 12.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Week)) Multiplier = new BigDecimal(1.0 / 52.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Day)) Multiplier = new BigDecimal(1.0 / 364.0);
    } else if (MeasureScope.equals(MEASURESCOPE_Quarter)) {
      if (MeasureDisplay.equals(MEASUREDISPLAY_Year)) Multiplier = new BigDecimal(4.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Month)) Multiplier = new BigDecimal(1.0 / 3.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Week)) Multiplier = new BigDecimal(1.0 / 13.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Day)) Multiplier = new BigDecimal(1.0 / 91.0);
    } else if (MeasureScope.equals(MEASURESCOPE_Month)) {
      if (MeasureDisplay.equals(MEASUREDISPLAY_Year)) Multiplier = new BigDecimal(12.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Quarter)) Multiplier = new BigDecimal(3.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Week)) Multiplier = new BigDecimal(1.0 / 4.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Day)) Multiplier = new BigDecimal(1.0 / 30.0);
    } else if (MeasureScope.equals(MEASURESCOPE_Week)) {
      if (MeasureDisplay.equals(MEASUREDISPLAY_Year)) Multiplier = new BigDecimal(52.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Quarter)) Multiplier = new BigDecimal(13.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Month)) Multiplier = new BigDecimal(4.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Day)) Multiplier = new BigDecimal(1.0 / 7.0);
    } else if (MeasureScope.equals(MEASURESCOPE_Day)) {
      if (MeasureDisplay.equals(MEASUREDISPLAY_Year)) Multiplier = new BigDecimal(364.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Quarter)) Multiplier = new BigDecimal(91.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Month)) Multiplier = new BigDecimal(30.0);
      else if (MeasureDisplay.equals(MEASUREDISPLAY_Week)) Multiplier = new BigDecimal(7.0);
    }
    return Multiplier;
  } //	getMultiplier

  /** Logger */
  private static CLogger s_log = CLogger.getCLogger(MGoal.class);

  /**
   * ************************************************************************ Standard Constructor
   *
   * @param ctx context
   * @param PA_Goal_ID id
   * @param trx p_trx
   */
  public MGoal(Ctx ctx, int PA_Goal_ID, Trx trx) {
    super(ctx, PA_Goal_ID, trx);
    if (PA_Goal_ID == 0) {
      //	setName (null);
      //	setAD_User_ID (0);
      //	setPA_ColorSchema_ID (0);
      setSeqNo(0);
      setIsSummary(false);
      setMeasureScope(MEASUREDISPLAY_Year);
      setGoalPerformance(Env.ZERO);
      setRelativeWeight(Env.ONE);
      setMeasureTarget(Env.ZERO);
      setMeasureActual(Env.ZERO);
    }
  } //	MGoal

  /**
   * Load Constructor
   *
   * @param ctx context
   * @param rs result set
   * @param trx p_trx
   */
  public MGoal(Ctx ctx, ResultSet rs, Trx trx) {
    super(ctx, rs, trx);
  } //	MGoal

  /**
   * Base Constructor
   *
   * @param ctx context
   * @param Name Name
   * @param Description Decsription
   * @param MeasureTarget target
   * @param trx p_trx
   */
  public MGoal(Ctx ctx, String Name, String Description, BigDecimal MeasureTarget, Trx trx) {
    super(ctx, 0, trx);
    setName(Name);
    setDescription(Description);
    setMeasureTarget(MeasureTarget);
  } //	MGoal

  /** Restrictions */
  private MGoalRestriction[] m_restrictions = 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;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, get_Trx());
      pstmt.setInt(1, getPA_Goal_ID());
      rs = pstmt.executeQuery();
      while (rs.next()) list.add(new MGoalRestriction(getCtx(), rs, get_Trx()));
    } catch (Exception e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeStatement(pstmt);
      DB.closeResultSet(rs);
    }
    //
    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_Trx());
        return true;
      }
    }
    return false;
  } //	updateGoal

  /**
   * Set Measure Actual
   *
   * @param MeasureActual actual
   */
  @Override
  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);
  } //	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 (getMeasureTarget().signum() == 0) return Color.white;
    else return MColorSchema.getColor(getCtx(), getPA_ColorSchema_ID(), getPercent());
  } //	getColor

  /**
   * Get the color schema for this goal.
   *
   * @return the color schema, or null if the measure targer is 0
   */
  public MColorSchema getColorSchema() {
    return (getMeasureTarget().signum() == 0)
        ? null
        : MColorSchema.get(getCtx(), getPA_ColorSchema_ID());
  }

  /**
   * Get Measure Display
   *
   * @return Measure Display
   */
  @Override
  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
        && X_PA_Measure.MEASUREDATATYPE_StatusQtyAmount.equals(measure.getMeasureDataType())) {
      if (X_PA_Measure.MEASURETYPE_Request.equals(measure.getMeasureType()))
        return Msg.getElement(getCtx(), "R_Status_ID");
      if (X_PA_Measure.MEASURETYPE_Project.equals(measure.getMeasureType()))
        return Msg.getElement(getCtx(), "C_Phase_ID");
    }
    String value = getMeasureDisplay();
    String display = MRefList.getListName(getCtx(), X_Ref_PA_Goal_Scope.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
   */
  @Override
  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
   */
  @Override
  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 (MRole element : roles) {
            if (AD_Role_ID == element.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
   */
  @Override
  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
Ejemplo n.º 15
0
/**
 * Time + Expense Line Model
 *
 * @author Jorg Janke
 * @version $Id: MTimeExpenseLine.java,v 1.4 2006/09/25 00:59:41 jjanke Exp $
 */
public class MTimeExpenseLine extends X_S_TimeExpenseLine {
  /** Logger for class MTimeExpenseLine */
  private static final org.compiere.util.CLogger log =
      org.compiere.util.CLogger.getCLogger(MTimeExpenseLine.class);
  /** */
  private static final long serialVersionUID = 1L;

  /**
   * Standard Constructor
   *
   * @param ctx context
   * @param S_TimeExpenseLine_ID id
   * @param trx transaction
   */
  public MTimeExpenseLine(Ctx ctx, int S_TimeExpenseLine_ID, Trx trx) {
    super(ctx, S_TimeExpenseLine_ID, trx);
    if (S_TimeExpenseLine_ID == 0) {
      //	setS_TimeExpenseLine_ID (0);		//	PK
      //	setS_TimeExpense_ID (0);			//	Parent
      setQty(Env.ONE);
      setQtyInvoiced(Env.ZERO);
      setQtyReimbursed(Env.ZERO);
      //
      setExpenseAmt(Env.ZERO);
      setConvertedAmt(Env.ZERO);
      setPriceReimbursed(Env.ZERO);
      setInvoicePrice(Env.ZERO);
      setPriceInvoiced(Env.ZERO);
      //
      setDateExpense(new Timestamp(System.currentTimeMillis()));
      setIsInvoiced(false);
      setIsTimeReport(false);
      setLine(10);
      setProcessed(false);
    }
  } //	MTimeExpenseLine

  /**
   * Load Constructor
   *
   * @param ctx context
   * @param rs result set
   * @param trx transaction
   */
  public MTimeExpenseLine(Ctx ctx, ResultSet rs, Trx trx) {
    super(ctx, rs, trx);
  } //	MTimeExpenseLine

  /** Currency of Report */
  private int m_C_Currency_Report_ID = 0;

  /**
   * Get Qty Invoiced
   *
   * @return entered or qty
   */
  @Override
  public BigDecimal getQtyInvoiced() {
    BigDecimal bd = super.getQtyInvoiced();
    if (Env.ZERO.compareTo(bd) == 0) return getQty();
    return bd;
  } //	getQtyInvoiced

  /**
   * Get Qty Reimbursed
   *
   * @return entered or qty
   */
  @Override
  public BigDecimal getQtyReimbursed() {
    BigDecimal bd = super.getQtyReimbursed();
    if (Env.ZERO.compareTo(bd) == 0) return getQty();
    return bd;
  } //	getQtyReimbursed

  /**
   * Get Price Invoiced
   *
   * @return entered or invoice price
   */
  @Override
  public BigDecimal getPriceInvoiced() {
    BigDecimal bd = super.getPriceInvoiced();
    if (Env.ZERO.compareTo(bd) == 0) return getInvoicePrice();
    return bd;
  } //	getPriceInvoiced

  /**
   * Get Price Reimbursed
   *
   * @return entered or converted amt
   */
  @Override
  public BigDecimal getPriceReimbursed() {
    BigDecimal bd = super.getPriceReimbursed();
    if (Env.ZERO.compareTo(bd) == 0) return getConvertedAmt();
    return bd;
  } //	getPriceReimbursed

  /**
   * Get Approval Amt
   *
   * @return qty * converted amt
   */
  public BigDecimal getApprovalAmt() {
    return getQty().multiply(getConvertedAmt());
  } //	getApprovalAmt

  /**
   * Get C_Currency_ID of Report (Price List)
   *
   * @return currency
   */
  public int getC_Currency_Report_ID() {
    if (m_C_Currency_Report_ID != 0) return m_C_Currency_Report_ID;
    //	Get it from header
    MTimeExpense te = new MTimeExpense(getCtx(), getS_TimeExpense_ID(), get_Trx());
    m_C_Currency_Report_ID = te.getC_Currency_ID();
    return m_C_Currency_Report_ID;
  } //	getC_Currency_Report_ID

  /**
   * Set C_Currency_ID of Report (Price List)
   *
   * @param C_Currency_ID currency
   */
  protected void setC_Currency_Report_ID(int C_Currency_ID) {
    m_C_Currency_Report_ID = C_Currency_ID;
  } //	getC_Currency_Report_ID

  /**
   * Set Resource Assignment - Callout
   *
   * @param oldS_ResourceAssignment_ID old value
   * @param newS_ResourceAssignment_ID new value
   * @param windowNo window
   * @throws Exception
   */
  @UICallout
  public void setS_ResourceAssignment_ID(
      String oldS_ResourceAssignment_ID, String newS_ResourceAssignment_ID, int windowNo)
      throws Exception {
    if (newS_ResourceAssignment_ID == null || newS_ResourceAssignment_ID.length() == 0) return;
    int S_ResourceAssignment_ID = Integer.parseInt(newS_ResourceAssignment_ID);
    if (S_ResourceAssignment_ID == 0) return;
    //
    super.setS_ResourceAssignment_ID(S_ResourceAssignment_ID);

    int M_Product_ID = 0;
    String Name = null;
    String Description = null;
    BigDecimal Qty = null;
    String sql =
        "SELECT p.M_Product_ID, ra.Name, ra.Description, ra.Qty "
            + "FROM S_ResourceAssignment ra"
            + " INNER JOIN M_Product p ON (p.S_Resource_ID=ra.S_Resource_ID) "
            + "WHERE ra.S_ResourceAssignment_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, get_Trx());
      pstmt.setInt(1, S_ResourceAssignment_ID);
      rs = pstmt.executeQuery();
      if (rs.next()) {
        M_Product_ID = rs.getInt(1);
        Name = rs.getString(2);
        Description = rs.getString(3);
        Qty = rs.getBigDecimal(4);
      }
    } catch (SQLException e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    log.fine(
        "S_ResourceAssignment_ID=" + S_ResourceAssignment_ID + " - M_Product_ID=" + M_Product_ID);
    if (M_Product_ID != 0) {
      setM_Product_ID(M_Product_ID);
      if (Description != null) Name += " (" + Description + ")";
      if (!".".equals(Name)) setDescription(Name);
      if (Qty != null) setQty(Qty);
    }
  } //	setS_ResourceAssignment_ID

  /**
   * Set Product - Callout
   *
   * @param oldM_Product_ID old value
   * @param newM_Product_ID new value
   * @param windowNo window
   * @throws Exception
   */
  @UICallout
  public void setM_Product_ID(String oldM_Product_ID, String newM_Product_ID, int windowNo)
      throws Exception {
    if (newM_Product_ID == null || newM_Product_ID.length() == 0) return;
    int M_Product_ID = Integer.parseInt(newM_Product_ID);

    super.setM_Product_ID(M_Product_ID);
    if (M_Product_ID == 0) return;

    //	Employee
    MTimeExpense hdr = new MTimeExpense(getCtx(), getS_TimeExpense_ID(), null);
    int C_BPartner_ID = hdr.getC_BPartner_ID();
    BigDecimal Qty = getQty();
    boolean IsSOTrx = true;
    MProductPricing pp =
        new MProductPricing(
            getAD_Client_ID(), getAD_Org_ID(), M_Product_ID, C_BPartner_ID, Qty, IsSOTrx);
    //
    int M_PriceList_ID = hdr.getM_PriceList_ID();
    pp.setM_PriceList_ID(M_PriceList_ID);
    Timestamp orderDate = getDateExpense();
    pp.setPriceDate(orderDate);
    //
    setExpenseAmt(pp.getPriceStd());
    setC_Currency_ID(pp.getC_Currency_ID());
    setAmt(windowNo, "M_Product_ID");
    setC_UOM_ID(pp.getC_UOM_ID()); // Setting UOM for the Selected product
  } //	setM_Product_ID

  /**
   * Set Currency - Callout
   *
   * @param oldC_Currency_ID old value
   * @param newC_Currency_ID new value
   * @param windowNo window
   * @throws Exception
   */
  @UICallout
  public void setC_Currency_ID(String oldC_Currency_ID, String newC_Currency_ID, int windowNo)
      throws Exception {
    if (newC_Currency_ID == null || newC_Currency_ID.length() == 0) return;
    int C_Currency_ID = Integer.parseInt(newC_Currency_ID);
    super.setC_Currency_ID(C_Currency_ID);
    setAmt(windowNo, "C_Currency_ID");
  } //	setC_Currency_ID

  /**
   * Set ExpenseAmt - Callout
   *
   * @param oldExpenseAmt old value
   * @param newExpenseAmt new value
   * @param windowNo window
   * @throws Exception
   */
  @UICallout
  public void setExpenseAmt(String oldExpenseAmt, String newExpenseAmt, int windowNo)
      throws Exception {
    if (newExpenseAmt == null || newExpenseAmt.length() == 0) return;
    BigDecimal ExpenseAmt = new BigDecimal(newExpenseAmt);
    super.setExpenseAmt(ExpenseAmt);
    setAmt(windowNo, "ExpenseAmt");
  } //	setExpenseAmt

  /**
   * Set Amount (Callout)
   *
   * @param windowNo window
   * @param columnName changed column
   */
  private void setAmt(int windowNo, String columnName) {
    //	get values
    BigDecimal ExpenseAmt = getExpenseAmt();
    int C_Currency_From_ID = getC_Currency_ID();
    int C_Currency_To_ID = getCtx().getContextAsInt("$C_Currency_ID");
    Timestamp DateExpense = getDateExpense();
    //
    log.fine("Amt=" + ExpenseAmt + ", C_Currency_ID=" + C_Currency_From_ID);
    //	Converted Amount = Unit price
    BigDecimal ConvertedAmt = ExpenseAmt;
    //	convert if required
    if (ConvertedAmt.signum() != 0 && C_Currency_To_ID != C_Currency_From_ID) {
      ConvertedAmt =
          MConversionRate.convert(
              getCtx(),
              ConvertedAmt,
              C_Currency_From_ID,
              C_Currency_To_ID,
              DateExpense,
              0,
              getAD_Client_ID(),
              getAD_Org_ID());
    }
    setConvertedAmt(ConvertedAmt);
    log.fine("ConvertedAmt=" + ConvertedAmt);
  } //	setAmt

  /**
   * Before Save. Calculate converted amt
   *
   * @param newRecord new
   * @return true
   */
  @Override
  protected boolean beforeSave(boolean newRecord) {
    //	Calculate Converted Amount
    if (newRecord || is_ValueChanged("ExpenseAmt") || is_ValueChanged("C_Currency_ID")) {
      if (getC_Currency_ID() == getC_Currency_Report_ID()) setConvertedAmt(getExpenseAmt());
      else {
        setConvertedAmt(
            MConversionRate.convert(
                getCtx(),
                getExpenseAmt(),
                getC_Currency_ID(),
                getC_Currency_Report_ID(),
                getDateExpense(),
                0,
                getAD_Client_ID(),
                getAD_Org_ID()));
      }
    }
    if (isTimeReport()) {
      setExpenseAmt(Env.ZERO);
      setConvertedAmt(Env.ZERO);
    }
    return true;
  } //	beforeSave

  /**
   * After Save
   *
   * @param newRecord new
   * @param success success
   * @return success
   */
  @Override
  protected boolean afterSave(boolean newRecord, boolean success) {
    if (success) {
      updateHeader();
      if (newRecord || is_ValueChanged("S_ResourceAssignment_ID")) {
        int S_ResourceAssignment_ID = getS_ResourceAssignment_ID();
        int old_S_ResourceAssignment_ID = 0;
        if (!newRecord) {
          Object ii = get_ValueOld("S_ResourceAssignment_ID");
          if (ii instanceof Integer) {
            old_S_ResourceAssignment_ID = ((Integer) ii).intValue();
            //	Changed Assignment
            if (old_S_ResourceAssignment_ID != S_ResourceAssignment_ID
                && old_S_ResourceAssignment_ID != 0) {
              MResourceAssignment ra =
                  new MResourceAssignment(getCtx(), old_S_ResourceAssignment_ID, get_Trx());
              ra.delete(false);
            }
          }
        }
        //	Sync Assignment
        if (S_ResourceAssignment_ID != 0) {
          MResourceAssignment ra =
              new MResourceAssignment(getCtx(), S_ResourceAssignment_ID, get_Trx());
          if (getQty().compareTo(ra.getQty()) != 0) {
            ra.setQty(getQty());
            if (getDescription() != null && getDescription().length() > 0)
              ra.setDescription(getDescription());
            ra.save();
          }
        }
      }
    }
    return success;
  } //	afterSave

  /**
   * After Delete
   *
   * @param success success
   * @return success
   */
  @Override
  protected boolean afterDelete(boolean success) {
    if (success) {
      updateHeader();
      //
      Object ii = get_ValueOld("S_ResourceAssignment_ID");
      if (ii instanceof Integer) {
        int old_S_ResourceAssignment_ID = ((Integer) ii).intValue();
        //	Deleted Assignment
        if (old_S_ResourceAssignment_ID != 0) {
          MResourceAssignment ra =
              new MResourceAssignment(getCtx(), old_S_ResourceAssignment_ID, get_Trx());
          ra.delete(false);
        }
      }
    }
    return success;
  } //	afterDelete

  /** Update Header. Set Approved Amount */
  private void updateHeader() {
    String sql =
        "UPDATE S_TimeExpense te"
            + " SET ApprovalAmt = "
            + "(SELECT SUM(Qty*ConvertedAmt) FROM S_TimeExpenseLine tel "
            + "WHERE te.S_TimeExpense_ID=tel.S_TimeExpense_ID) "
            + "WHERE S_TimeExpense_ID=? ";
    DB.executeUpdate(get_Trx(), sql, getS_TimeExpense_ID());

    if (get_Trx() != null) get_Trx().commit();
  } //	updateHeader
} //	MTimeExpenseLine
Ejemplo n.º 16
0
/**
 * Calendar Period Model
 *
 * @author Jorg Janke
 * @version $Id: MPeriod.java,v 1.4 2006/07/30 00:51:05 jjanke Exp $
 */
public class MPeriod extends X_C_Period {
  /** Logger for class MPeriod */
  private static final org.compiere.util.CLogger log =
      org.compiere.util.CLogger.getCLogger(MPeriod.class);
  /** */
  private static final long serialVersionUID = 1L;

  /**
   * Get Period from Cache
   *
   * @param ctx context
   * @param C_Period_ID id
   * @return MPeriod
   */
  public static MPeriod get(Ctx ctx, int C_Period_ID) {
    Integer key = Integer.valueOf(C_Period_ID);
    MPeriod retValue = s_cache.get(ctx, key);
    if (retValue != null) return retValue;
    //
    retValue = new MPeriod(ctx, C_Period_ID, null);
    if (retValue.get_ID() != 0) s_cache.put(key, retValue);
    return retValue;
  } //	get

  /**
   * Find standard Period of DateAcct based on Client Calendar
   *
   * @param ctx context
   * @param DateAcct date
   * @return active Period or null
   */
  public static MPeriod getOfOrg(Ctx ctx, int AD_Org_ID, Timestamp DateAcct) {
    int C_Calendar_ID = 0;
    if (AD_Org_ID != 0) {
      MOrgInfo info = MOrgInfo.get(ctx, AD_Org_ID, null);
      C_Calendar_ID = info.getC_Calendar_ID();
    }
    if (C_Calendar_ID == 0) {
      MClientInfo cInfo = MClientInfo.get(ctx);
      C_Calendar_ID = cInfo.getC_Calendar_ID();
    }

    return getOfCalendar(ctx, C_Calendar_ID, DateAcct);
  } //	get

  /**
   * Find standard Period of DateAcct based on Client Calendar
   *
   * @param ctx context
   * @param C_Calendar_ID calendar
   * @param DateAcct date
   * @return active Period or null
   */
  public static MPeriod getOfCalendar(Ctx ctx, int C_Calendar_ID, Timestamp DateAcct) {
    if (DateAcct == null) {
      s_log.warning("No DateAcct");
      return null;
    }
    if (C_Calendar_ID == 0) {
      s_log.warning("No Calendar");
      return null;
    }
    //	Search in Cache first
    Iterator<MPeriod> it = s_cache.values().iterator();
    while (it.hasNext()) {
      MPeriod period = it.next();
      if (period.getC_Calendar_ID() == C_Calendar_ID
          && period.isStandardPeriod()
          && period.isInPeriod(DateAcct)) return period;
    }

    //	Get it from DB
    MPeriod retValue = null;
    String sql =
        "SELECT * FROM C_Period "
            + "WHERE C_Year_ID IN "
            + "(SELECT C_Year_ID FROM C_Year WHERE C_Calendar_ID=?)"
            + " AND ? BETWEEN TRUNC(StartDate,'DD') AND TRUNC(EndDate,'DD')"
            + " AND IsActive='Y' AND PeriodType='S'";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, (Trx) null);
      pstmt.setInt(1, C_Calendar_ID);
      pstmt.setTimestamp(2, TimeUtil.getDay(DateAcct));
      rs = pstmt.executeQuery();
      while (rs.next()) {
        MPeriod period = new MPeriod(ctx, rs, null);
        Integer key = Integer.valueOf(period.getC_Period_ID());
        s_cache.put(key, period);
        if (period.isStandardPeriod()) retValue = period;
      }
    } catch (SQLException e) {
      s_log.log(Level.SEVERE, "DateAcct=" + DateAcct, e);
    } finally {
      DB.closeStatement(pstmt);
      DB.closeResultSet(rs);
    }
    if (retValue == null)
      s_log.warning(
          "No Standard Period for " + DateAcct + " (C_Calendar_ID=" + C_Calendar_ID + ")");
    return retValue;
  } //	get

  /**
   * Find valid standard Period of DateAcct based on Client Calendar
   *
   * @param ctx context
   * @param DateAcct date
   * @return C_Period_ID or 0
   */
  public static int getC_Period_ID(Ctx ctx, int AD_Org_ID, Timestamp DateAcct) {
    MPeriod period = getOfOrg(ctx, AD_Org_ID, DateAcct);
    if (period == null) return 0;
    return period.getC_Period_ID();
  } //	getC_Period_ID

  /**
   * Is standard Period Open for Document Base Type - does not check Orgs
   *
   * @param ctx context
   * @param DateAcct date
   * @param DocBaseType base type
   * @return true if open
   * @deprecated use new isOpen
   */
  @Deprecated
  public static boolean isOpenOld(Ctx ctx, Timestamp DateAcct, String DocBaseType) {
    if (DateAcct == null) {
      s_log.warning("No DateAcct");
      return false;
    }
    if (DocBaseType == null) {
      s_log.warning("No DocBaseType");
      return false;
    }
    MPeriod period = MPeriod.getOfOrg(ctx, 0, DateAcct);
    if (period == null) {
      s_log.warning("No Period for " + DateAcct + " (" + DocBaseType + ")");
      return false;
    }
    String error = period.isOpen(DocBaseType, DateAcct);
    if (error != null) s_log.warning(error + " - " + period.getName());
    return error == null;
  } //	isOpen

  /**
   * Is standard Period Open for specified orgs for the client. For best performance, ensure that
   * the list of orgs does not contain duplicates.
   *
   * @param ctx
   * @param AD_Client_ID
   * @param orgs
   * @param DateAcct accounting date
   * @param DocBaseType document base type
   * @return error message or null
   */
  public static String isOpen(
      Ctx ctx, int AD_Client_ID, ArrayList<Integer> orgs, Timestamp DateAcct, String DocBaseType) {
    if (DateAcct == null) return "@NotFound@ @DateAcct@";
    if (DocBaseType == null) return "@NotFound@ @DocBaseType@";

    MAcctSchema as = MClient.get(ctx, AD_Client_ID).getAcctSchema();
    if (as == null) return "@NotFound@ @C_AcctSchema_ID@ for AD_Client_ID=" + AD_Client_ID;
    if (as.isAutoPeriodControl()) {
      if (as.isAutoPeriodControlOpen(DateAcct)) return null;
      else return "@PeriodClosed@ - @AutoPeriodControl@";
    }

    //	Get all Calendars in line with Organizations
    MClientInfo clientInfo = MClientInfo.get(ctx, AD_Client_ID, null);
    ArrayList<Integer> orgCalendars = new ArrayList<Integer>();
    ArrayList<Integer> calendars = new ArrayList<Integer>();
    for (int org : orgs) {
      MOrgInfo orgInfo = MOrgInfo.get(ctx, org, null);
      int C_Calendar_ID = orgInfo.getC_Calendar_ID();
      if (C_Calendar_ID == 0) C_Calendar_ID = clientInfo.getC_Calendar_ID();
      orgCalendars.add(C_Calendar_ID);
      if (!calendars.contains(C_Calendar_ID)) calendars.add(C_Calendar_ID);
    }
    //	Should not happen
    if (calendars.size() == 0) return "@NotFound@ @C_Calendar_ID@";

    //	For all Calendars get Periods
    for (int i = 0; i < calendars.size(); i++) {
      int C_Calendar_ID = calendars.get(i);
      MPeriod period = MPeriod.getOfCalendar(ctx, C_Calendar_ID, DateAcct);
      //	First Org for Calendar
      int AD_Org_ID = 0;
      for (int j = 0; j < orgCalendars.size(); j++) {
        if (orgCalendars.get(j) == C_Calendar_ID) {
          AD_Org_ID = orgs.get(j);
          break;
        }
      }
      if (period == null) {
        MCalendar cal = MCalendar.get(ctx, C_Calendar_ID);
        String date = DisplayType.getDateFormat(DisplayTypeConstants.Date).format(DateAcct);
        if (cal != null)
          return "@NotFound@ @C_Period_ID@: "
              + date
              + " - "
              + MOrg.get(ctx, AD_Org_ID).getName()
              + " -> "
              + cal.getName();
        else
          return "@NotFound@ @C_Period_ID@: "
              + date
              + " - "
              + MOrg.get(ctx, AD_Org_ID).getName()
              + " -> C_Calendar_ID="
              + C_Calendar_ID;
      }
      String error = period.isOpen(DocBaseType, DateAcct);
      if (error != null)
        return error
            + " - "
            + MOrg.get(ctx, AD_Org_ID).getName()
            + " -> "
            + MCalendar.get(ctx, C_Calendar_ID).getName();
    }
    return null; //	open
  } //	isOpen

  /**
   * Is standard Period Open for Document Base Type
   *
   * @param header header document record
   * @param lines document lines optional
   * @param DateAcct accounting date
   * @param DocBaseType document base type
   * @return error message or null
   */
  @Deprecated
  public static String isOpen(PO header, PO[] lines, Timestamp DateAcct, String DocBaseType) {

    // Get All Orgs
    ArrayList<Integer> orgs = new ArrayList<Integer>();
    orgs.add(header.getAD_Org_ID());
    if (lines != null) {
      for (PO line : lines) {
        int AD_Org_ID = line.getAD_Org_ID();
        if (!orgs.contains(AD_Org_ID)) orgs.add(AD_Org_ID);
      }
    }

    return isOpen(header.getCtx(), header.getAD_Client_ID(), orgs, DateAcct, DocBaseType);
  } // isOpen

  /**
   * Is standard Period closed for all Document Base Types
   *
   * @param ctx context for AD_Client
   * @param DateAcct accounting date
   * @return true if closed
   */
  public static boolean isClosed(Ctx ctx, Timestamp DateAcct) {
    if (DateAcct == null) return false;
    MAcctSchema as = MClient.get(ctx, ctx.getAD_Client_ID()).getAcctSchema();
    if (as.isAutoPeriodControl()) return !as.isAutoPeriodControlOpen(DateAcct);

    //	Get all Calendars in line with Organizations
    MClientInfo cInfo = MClientInfo.get(ctx, ctx.getAD_Client_ID(), null);
    ArrayList<Integer> calendars = new ArrayList<Integer>();
    MOrg[] orgs = MOrg.getOfClient(cInfo);
    for (MOrg org : orgs) {
      MOrgInfo info = MOrgInfo.get(ctx, org.getAD_Org_ID(), null);
      int C_Calendar_ID = info.getC_Calendar_ID();
      if (C_Calendar_ID == 0) C_Calendar_ID = cInfo.getC_Calendar_ID();
      if (!calendars.contains(C_Calendar_ID)) calendars.add(C_Calendar_ID);
    }
    //	Should not happen
    if (calendars.size() == 0) throw new IllegalArgumentException("@NotFound@ @C_Calendar_ID@");

    //	For all Calendars get Periods
    for (int i = 0; i < calendars.size(); i++) {
      int C_Calendar_ID = calendars.get(i);
      MPeriod period = MPeriod.getOfCalendar(ctx, C_Calendar_ID, DateAcct);
      //	Period not found
      if (period == null) return false;
      if (!period.isClosed()) return false;
    }
    return true; //	closed
  } //	isClosed

  /**
   * Find first Year Period of DateAcct based on Client Calendar
   *
   * @param ctx context
   * @param C_Calendar_ID calendar
   * @param DateAcct date
   * @return active first Period
   */
  public static MPeriod getFirstInYear(Ctx ctx, int C_Calendar_ID, Timestamp DateAcct) {
    MPeriod retValue = null;
    String sql =
        "SELECT * "
            + "FROM C_Period "
            + "WHERE C_Year_ID IN "
            + "(SELECT p.C_Year_ID "
            + "FROM C_Year y"
            + " INNER JOIN C_Period p ON (y.C_Year_ID=p.C_Year_ID) "
            + "WHERE y.C_Calendar_ID=?"
            + "	AND ? BETWEEN StartDate AND EndDate)"
            + " AND IsActive='Y' AND PeriodType='S' "
            + "ORDER BY StartDate";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, (Trx) null);
      pstmt.setInt(1, C_Calendar_ID);
      pstmt.setTimestamp(2, DateAcct);
      rs = pstmt.executeQuery();
      if (rs.next()) // 	first only
      retValue = new MPeriod(ctx, rs, null);
    } catch (SQLException e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeStatement(pstmt);
      DB.closeResultSet(rs);
    }
    return retValue;
  } //	getFirstInYear

  /** Cache */
  private static final CCache<Integer, MPeriod> s_cache =
      new CCache<Integer, MPeriod>("C_Period", 10);

  /** Logger */
  private static final CLogger s_log = CLogger.getCLogger(MPeriod.class);

  /**
   * ************************************************************************ Standard Constructor
   *
   * @param ctx context
   * @param C_Period_ID id
   * @param trx transaction
   */
  public MPeriod(Ctx ctx, int C_Period_ID, Trx trx) {
    super(ctx, C_Period_ID, trx);
    if (C_Period_ID == 0) {
      //	setC_Period_ID (0);		//	PK
      //  setC_Year_ID (0);		//	Parent
      //  setName (null);
      //  setPeriodNo (0);
      //  setStartDate (new Timestamp(System.currentTimeMillis()));
      setPeriodType(PERIODTYPE_StandardCalendarPeriod);
    }
  } //	MPeriod

  /**
   * Load Constructor
   *
   * @param ctx context
   * @param rs result set
   * @param trx transaction
   */
  public MPeriod(Ctx ctx, ResultSet rs, Trx trx) {
    super(ctx, rs, trx);
  } //	MPeriod

  /**
   * Parent constructor
   *
   * @param year year
   * @param PeriodNo no
   * @param name name
   * @param startDate start
   * @param endDate end
   */
  public MPeriod(MYear year, int PeriodNo, String name, Timestamp startDate, Timestamp endDate) {
    this(year.getCtx(), 0, year.get_Trx());
    setClientOrg(year);
    setC_Year_ID(year.getC_Year_ID());
    setPeriodNo(PeriodNo);
    setName(name);
    setStartDate(startDate);
    setEndDate(endDate);
  } //	MPeriod

  /** Period Controls */
  private MPeriodControl[] m_controls = null;
  /** Calendar */
  private int m_C_Calendar_ID = 0;

  /**
   * Get Period Control
   *
   * @param requery requery
   * @return period controls
   */
  public MPeriodControl[] getPeriodControls(boolean requery) {
    if (m_controls != null && !requery) return m_controls;
    //
    ArrayList<MPeriodControl> list = new ArrayList<MPeriodControl>();
    String sql = "SELECT * FROM C_PeriodControl " + "WHERE C_Period_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, get_Trx());
      pstmt.setInt(1, getC_Period_ID());
      rs = pstmt.executeQuery();
      while (rs.next()) list.add(new MPeriodControl(getCtx(), rs, get_Trx()));
    } catch (Exception e) {
      log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    m_controls = new MPeriodControl[list.size()];
    list.toArray(m_controls);
    return m_controls;
  } //	getPeriodControls

  /**
   * Get Period Control
   *
   * @param DocBaseType Document Base Type
   * @return period control or null
   */
  public MPeriodControl getPeriodControl(String DocBaseType) {
    if (DocBaseType == null) return null;
    getPeriodControls(false);
    for (MPeriodControl element : m_controls) {
      //	log.fine("getPeriodControl - " + 1 + " - " + m_controls[i]);
      if (DocBaseType.equals(element.getDocBaseType())) return element;
    }
    return null;
  } //	getPeriodControl

  /**
   * Date In Period
   *
   * @param date date
   * @return true if in period
   */
  public boolean isInPeriod(Timestamp date) {
    if (date == null) return false;
    Timestamp dateOnly = TimeUtil.getDay(date);
    Timestamp from = TimeUtil.getDay(getStartDate());
    if (dateOnly.before(from)) return false;
    Timestamp to = TimeUtil.getDay(getEndDate());
    if (dateOnly.after(to)) return false;
    return true;
  } //	isInPeriod

  /**
   * Is Period Open for Doc Base Type
   *
   * @param DocBaseType document base type
   * @param dateAcct accounting date
   * @return error message or null
   */
  public String isOpen(String DocBaseType, Timestamp dateAcct) {
    if (!isActive()) {
      s_log.warning("Period not active: " + getName());
      return "@C_Period_ID@ <> @IsActive@";
    }

    MAcctSchema as = MClient.get(getCtx(), getAD_Client_ID()).getAcctSchema();
    if (as != null && as.isAutoPeriodControl()) {
      if (!as.isAutoPeriodControlOpen(dateAcct)) return "@PeriodClosed@ - @AutoPeriodControl@";
      //	We are OK
      Timestamp today = new Timestamp(System.currentTimeMillis());
      if (isInPeriod(today) && as.getC_Period_ID() != getC_Period_ID()) {
        as.setC_Period_ID(getC_Period_ID());
        as.save();
      }
      return null;
    }

    //	Standard Period Control
    if (DocBaseType == null) {
      log.warning(getName() + " - No DocBaseType");
      return "@NotFound@ @DocBaseType@";
    }
    MPeriodControl pc = getPeriodControl(DocBaseType);
    if (pc == null) {
      log.warning(getName() + " - Period Control not found for " + DocBaseType);
      return "@NotFound@ @C_PeriodControl_ID@: " + DocBaseType;
    }
    log.fine(getName() + ": " + DocBaseType);
    if (pc.isOpen()) return null;
    return "@PeriodClosed@ - @C_PeriodControl_ID@ (" + DocBaseType + ", " + dateAcct + ")";
  } //	isOpen

  /**
   * Return true if all PC are closed
   *
   * @return true if closed
   */
  public boolean isClosed() {
    MPeriodControl[] pcs = getPeriodControls(false);
    for (MPeriodControl pc : pcs) {
      if (!pc.isClosed()) return false;
    }
    return true;
  } //	isClosed

  /**
   * Standard Period
   *
   * @return true if standard calendar period
   */
  public boolean isStandardPeriod() {
    return PERIODTYPE_StandardCalendarPeriod.equals(getPeriodType());
  } //	isStandardPeriod

  /**
   * Get Calendar of Period
   *
   * @return calendar
   */
  public int getC_Calendar_ID() {
    if (m_C_Calendar_ID == 0) {
      MYear year = MYear.get(getCtx(), getC_Year_ID());
      if (year != null) m_C_Calendar_ID = year.getC_Calendar_ID();
      else log.severe("@NotFound@ C_Year_ID=" + getC_Year_ID());
    }
    return m_C_Calendar_ID;
  } //	getC_Calendar_ID

  /**
   * Before Save. Truncate Dates
   *
   * @param newRecord new
   * @return true
   */
  @Override
  protected boolean beforeSave(boolean newRecord) {
    Timestamp startdate = getStartDate();
    Timestamp enddate = getEndDate();

    if (enddate != null && startdate.after(enddate)) {

      s_log.saveError("Error", Msg.getMsg(getCtx(), "CalPeriodInvalidDate"));
      return false;
    }
    //	Truncate Dates
    startdate = TimeUtil.getDay(startdate);
    setStartDate(startdate);

    if (enddate != null) enddate = TimeUtil.getDay(enddate);
    else enddate = TimeUtil.getMonthLastDay(getStartDate());

    //		Adding the time component of 23:59:59 to the end date
    enddate = new Timestamp(enddate.getTime() + 86399000);
    setEndDate(enddate);

    MPeriod[] periods = getAllPeriodsInYear(getC_Year_ID(), "S", getCtx(), get_Trx());
    MPeriod[] allperiods = getAllPeriodsInCalendar(getC_Calendar_ID(), "S", getCtx(), get_Trx());
    //		Check for non-negative period number
    if (getPeriodNo() < 0) {
      s_log.saveError("Error", Msg.getMsg(getCtx(), "CalNegPeriodNo"));
      return false;
    }

    //		Check for standard period
    if (isStandardPeriod() == true) {
      // Check Period number is in ascending order

      Timestamp nextPeriodStartDate = null;
      Timestamp prevPeriodStartDate = null;

      // Get the next standard period number Start Date in this year
      String sql =
          "SELECT StartDate FROM C_Period WHERE "
              + "C_Period.IsActive='Y' AND PeriodType='S' "
              + "AND C_Period.C_Year_ID =? "
              + "AND C_Period.C_Period_ID <> ?"
              + "AND  C_Period.PeriodNo "
              + " >  ?  ORDER BY  C_Period.PeriodNo ASC";
      Object[][] result = null;
      result =
          QueryUtil.executeQuery(get_Trx(), sql, getC_Year_ID(), getC_Period_ID(), getPeriodNo());

      if (result.length != 0) nextPeriodStartDate = (Timestamp) result[0][0];

      // Get the previous standard period number Start Date in this year
      sql =
          "SELECT StartDate FROM C_Period WHERE "
              + "C_Period.IsActive='Y' AND PeriodType='S'  "
              + "AND C_Period.C_Year_ID =? "
              + "AND C_Period.C_Period_ID <> ?"
              + "AND C_Period.PeriodNo "
              + "< ?  ORDER BY  C_Period.PeriodNo DESC";

      result =
          QueryUtil.executeQuery(get_Trx(), sql, getC_Year_ID(), getC_Period_ID(), getPeriodNo());
      if (result.length != 0) prevPeriodStartDate = (Timestamp) result[0][0];

      if ((prevPeriodStartDate != null
          && TimeUtil.max(prevPeriodStartDate, startdate) == prevPeriodStartDate)) {
        s_log.saveError("Error", Msg.getMsg(getCtx(), "CalPeriodAsc"));
        return false;
      }

      if ((nextPeriodStartDate != null
          && TimeUtil.max(nextPeriodStartDate, startdate) == startdate)) {
        s_log.saveError("Error", Msg.getMsg(getCtx(), "CalPeriodAsc"));
        return false;
      }

      //  Check if the Standard Period is overlapping other periods.

      for (MPeriod period : allperiods) {
        if ((TimeUtil.isValid(period.getStartDate(), period.getEndDate(), startdate) == true
                || TimeUtil.isValid(period.getStartDate(), period.getEndDate(), enddate) == true)
            && period.getC_Period_ID() != getC_Period_ID()) {
          s_log.saveError("Error", Msg.getMsg(getCtx(), "CalPeriodOverlap"));
          return false;
        }
      }

    }
    //		Check for adjusting period
    else {
      boolean startflag = false;
      boolean endflag = false;
      for (MPeriod period : periods) {
        if (TimeUtil.isValid(period.getStartDate(), period.getEndDate(), startdate) == true)
          startflag = true;
        if (TimeUtil.isValid(period.getStartDate(), period.getEndDate(), enddate) == true)
          endflag = true;
        if (startflag == true && endflag == true) break;
      }
      if (startflag == false || endflag == false) {
        s_log.saveError("Error", Msg.getMsg(getCtx(), "CalAdjPeriod"));
        return false;
      }
    }
    return true;
  } //	beforeSave

  /**
   * After Save
   *
   * @param newRecord new
   * @param success success
   * @return success
   */
  @Override
  protected boolean afterSave(boolean newRecord, boolean success) {
    if (newRecord) {
      //	SELECT Value FROM AD_Ref_List WHERE AD_Reference_ID=183
      MDocType[] types = MDocType.getOfClient(getCtx());
      int count = 0;
      ArrayList<String> baseTypes = new ArrayList<String>();
      for (MDocType type : types) {
        String DocBaseType = type.getDocBaseType();
        if (baseTypes.contains(DocBaseType)) continue;
        MPeriodControl pc = new MPeriodControl(this, DocBaseType);
        if (pc.save()) count++;
        baseTypes.add(DocBaseType);
      }
      log.fine("PeriodControl #" + count);
    }
    return success;
  } //	afterSave

  /**
   * String Representation
   *
   * @return info
   */
  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer("MPeriod[");
    sb.append(get_ID())
        .append("-")
        .append(getName())
        .append(", ")
        .append(getStartDate())
        .append("-")
        .append(getEndDate())
        .append("]");
    return sb.toString();
  } //	toString

  /**
   * Returns the next period forward
   *
   * @param period MPeriod
   * @param trx trx
   * @param ctx Ctx
   * @return MPeriod
   */
  public static MPeriod getNextPeriod(MPeriod period, Ctx ctx, Trx trx) {

    MPeriod newPeriod = null;
    String sql =
        "SELECT * FROM C_Period WHERE "
            + "C_Period.IsActive='Y' AND PeriodType='S' "
            + "AND C_Period.C_Year_ID IN "
            + "(SELECT C_Year_ID FROM C_Year WHERE C_Year.C_Calendar_ID = ? ) "
            + "AND ((C_Period.C_Year_ID * 1000) + C_Period.PeriodNo) "
            + " > ((? * 1000) + ?) ORDER BY C_Period.C_Year_ID ASC, C_Period.PeriodNo ASC";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, period.getC_Calendar_ID());
      pstmt.setInt(2, period.getC_Year_ID());
      pstmt.setInt(3, period.getPeriodNo());
      rs = pstmt.executeQuery();
      if (rs.next()) newPeriod = new MPeriod(ctx, rs, trx);
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    return newPeriod;
  }

  /**
   * Returns the previous period
   *
   * @param period MPeriod
   * @param periodCount Count
   * @param trx trx
   * @param ctx Ctx
   * @return MPeriod
   */
  public static MPeriod getPreviousPeriod(MPeriod period, Ctx ctx, Trx trx) {

    MPeriod newPeriod = null;
    String sql =
        "SELECT * FROM C_Period WHERE "
            + "C_Period.IsActive='Y' AND PeriodType='S' "
            + "AND C_Period.C_Year_ID IN "
            + "(SELECT C_Year_ID FROM C_Year WHERE C_Year.C_Calendar_ID = ? ) "
            + "AND ((C_Period.C_Year_ID * 1000) + C_Period.PeriodNo) "
            + " < ((? * 1000) + ?) ORDER BY C_Period.C_Year_ID DESC, C_Period.PeriodNo DESC";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, period.getC_Calendar_ID());
      pstmt.setInt(2, period.getC_Year_ID());
      pstmt.setInt(3, period.getPeriodNo());
      rs = pstmt.executeQuery();
      if (rs.next()) newPeriod = new MPeriod(ctx, rs, trx);
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    return newPeriod;
  }

  /**
   * gets all Periods in the Range
   *
   * @param startPeriod
   * @param endPeriod
   * @param calendar_ID
   * @return MPeriod[]
   */
  public static MPeriod[] getAllPeriodsInRange(
      MPeriod startPeriod, MPeriod endPeriod, int calendar_ID, Ctx ctx, Trx trx) {
    if ((startPeriod.getC_Calendar_ID() != calendar_ID)
        || (endPeriod.getC_Calendar_ID() != calendar_ID)) {
      log.saveError("Error", "Periods do not belong to the calendar");
      return null;
    }

    ArrayList<MPeriod> periods = new ArrayList<MPeriod>();
    String sql =
        "SELECT * FROM C_Period WHERE "
            + "C_Period.IsActive='Y' AND PeriodType='S' "
            + "AND C_Period.C_Year_ID IN "
            + "(SELECT C_Year_ID FROM C_Year WHERE C_Year.C_Calendar_ID = ? ) "
            + // calendar_ID
            "AND ((C_Period.C_Year_ID * 1000) + C_Period.PeriodNo) BETWEEN"
            + " (? * 1000 + ?) AND (? * 1000 + ? )"
            + // start Period year ID, Period Number , End Period Year ID, Period Number
            " ORDER BY C_Period.C_Year_ID ASC, C_Period.PeriodNo ASC";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, calendar_ID);
      pstmt.setInt(2, startPeriod.getC_Year_ID());
      pstmt.setInt(3, startPeriod.getPeriodNo());
      pstmt.setInt(4, endPeriod.getC_Year_ID());
      pstmt.setInt(5, endPeriod.getPeriodNo());
      rs = pstmt.executeQuery();
      while (rs.next()) periods.add(new MPeriod(ctx, rs, trx));
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    MPeriod[] retValue = new MPeriod[periods.size()];
    periods.toArray(retValue);
    return retValue;
  }

  /**
   * Find Period of Date based on Client Calendar, it need not be a standard period (used in MRP)
   *
   * @param ctx context
   * @param C_Calendar_ID calendar
   * @param Date date
   * @param trx trx
   * @return active Period or null
   */
  public static MPeriod getPeriod(Ctx ctx, int C_Calendar_ID, Timestamp Date, Trx trx) {
    if (Date == null) {
      s_log.warning("No Date");
      return null;
    }
    if (C_Calendar_ID == 0) {
      s_log.warning("No Calendar");
      return null;
    }

    //	Get it from DB
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    MPeriod retValue = null;
    String sql =
        "SELECT * FROM C_Period "
            + "WHERE C_Year_ID IN "
            + "(SELECT C_Year_ID FROM C_Year WHERE C_Calendar_ID=?)"
            + " AND ? BETWEEN TRUNC(StartDate,'DD') AND TRUNC(EndDate,'DD')"
            + " AND IsActive='Y' ";
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, C_Calendar_ID);
      pstmt.setTimestamp(2, TimeUtil.getDay(Date));
      rs = pstmt.executeQuery();
      if (rs.next()) {
        retValue = new MPeriod(ctx, rs, trx);
      }
    } catch (SQLException e) {
      s_log.log(Level.SEVERE, "DateAcct=" + Date, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    if (retValue == null)
      s_log.warning("No Period for " + Date + " (C_Calendar_ID=" + C_Calendar_ID + ")");
    return retValue;
  } //	getPeriod

  /**
   * Find the periods in a calendar it need not be a standard period (used in MRP)
   *
   * @param C_Calendar_ID calendar
   * @param periodType Period Type
   * @param ctx context
   * @param trx trx
   * @return MPeriod[]
   */
  public static MPeriod[] getAllPeriodsInCalendar(
      int C_Calendar_ID, String periodType, Ctx ctx, Trx trx) {

    List<MPeriod> periods = new ArrayList<MPeriod>();
    String sql = "SELECT * FROM C_Period WHERE IsActive='Y'";

    sql = sql + " AND C_Year_ID IN ( SELECT C_Year_ID FROM C_Year WHERE C_Calendar_ID=?)";

    if (periodType != null) sql = sql + " AND PeriodType = ? ";

    sql = sql + " ORDER BY StartDate ";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, C_Calendar_ID);

      if (periodType != null) pstmt.setString(2, periodType);

      rs = pstmt.executeQuery();
      while (rs.next()) periods.add(new MPeriod(ctx, rs, trx));
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    MPeriod[] retValue = new MPeriod[periods.size()];
    periods.toArray(retValue);
    return retValue;
  }

  /**
   * Find the periods in a calendar year it need not be a standard period (used in MRP)
   *
   * @param C_Year_ID Year
   * @param periodType Period Type
   * @param ctx context
   * @param trx trx
   * @return MPeriod[]
   */
  public static MPeriod[] getAllPeriodsInYear(int C_Year_ID, String periodType, Ctx ctx, Trx trx) {

    List<MPeriod> periods = new ArrayList<MPeriod>();
    String sql = "SELECT * FROM C_Period WHERE IsActive='Y'";

    sql = sql + " AND C_Year_ID = ?";

    if (periodType != null) sql = sql + " AND PeriodType = ? ";

    sql = sql + " order by StartDate ";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, C_Year_ID);

      if (periodType != null) pstmt.setString(2, periodType);

      rs = pstmt.executeQuery();
      while (rs.next()) periods.add(new MPeriod(ctx, rs, trx));
    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {
      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }
    MPeriod[] retValue = new MPeriod[periods.size()];
    periods.toArray(retValue);
    return retValue;
  }

  /**
   * Find all the year records in a Calendar, it need not be a standard period (used in MRP)
   *
   * @param C_Calendar_ID calendar
   * @param ctx context
   * @param trx trx
   * @return MYear[]
   */
  public static MYear[] getAllYearsInCalendar(int C_Calendar_ID, Ctx ctx, Trx trx) {

    List<MYear> years = new ArrayList<MYear>();
    String sql = "SELECT * FROM C_Year WHERE " + "IsActive='Y' AND C_Calendar_ID = ? ";

    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      pstmt = DB.prepareStatement(sql, trx);
      pstmt.setInt(1, C_Calendar_ID);
      rs = pstmt.executeQuery();
      while (rs.next()) years.add(new MYear(ctx, rs, trx));

    } catch (Exception e) {
      s_log.log(Level.SEVERE, sql, e);
    } finally {

      DB.closeResultSet(rs);
      DB.closeStatement(pstmt);
    }

    MYear[] retValue = new MYear[years.size()];
    years.toArray(retValue);
    return retValue;
  }
} //	MPeriod
Ejemplo n.º 17
0
 /**
  * Execute Task locally and wait
  *
  * @param cmd command
  * @return execution info
  */
 public String executeRemote(String cmd) {
   log.config(cmd);
   return "Remote:\n";
 } //	executeRemote