Search in sources :

Example 1 with MPeriodControl

use of org.compiere.model.MPeriodControl in project adempiere by adempiere.

the class DocumentTypeVerify method createPeriodControls.

//	createDocumentTypes
/**
	 * 	Create Period Controls
	 * 	@param ctx context
	 * 	@param AD_Client_ID client
	 * 	@param sp server process
	 *	@param trxName transaction
	 */
public static void createPeriodControls(Properties ctx, int AD_Client_ID, SvrProcess sp, String trxName) {
    s_log.info("AD_Client_ID=" + AD_Client_ID);
    //	Delete Duplicates
    String sql = "DELETE C_PeriodControl pc1 " + "WHERE (C_Period_ID, DocBaseType) IN " + "(SELECT C_Period_ID, DocBaseType " + "FROM C_PeriodControl pc2 " + "GROUP BY C_Period_ID, DocBaseType " + "HAVING COUNT(*) > 1)" + " AND C_PeriodControl_ID NOT IN " + "(SELECT MIN(C_PeriodControl_ID) " + "FROM C_PeriodControl pc3 " + "GROUP BY C_Period_ID, DocBaseType)";
    int no = DB.executeUpdate(sql, false, trxName);
    s_log.info("Duplicates deleted #" + no);
    //	Insert Missing
    sql = "SELECT DISTINCT p.AD_Client_ID, p.C_Period_ID, dt.DocBaseType " + "FROM C_Period p" + " FULL JOIN C_DocType dt ON (p.AD_Client_ID=dt.AD_Client_ID) " + "WHERE p.AD_Client_ID=?" + " AND NOT EXISTS" + " (SELECT * FROM C_PeriodControl pc " + "WHERE pc.C_Period_ID=p.C_Period_ID AND pc.DocBaseType=dt.DocBaseType)";
    PreparedStatement pstmt = null;
    int counter = 0;
    try {
        pstmt = DB.prepareStatement(sql, trxName);
        pstmt.setInt(1, AD_Client_ID);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int Client_ID = rs.getInt(1);
            int C_Period_ID = rs.getInt(2);
            String DocBaseType = rs.getString(3);
            s_log.config("AD_Client_ID=" + Client_ID + ", C_Period_ID=" + C_Period_ID + ", DocBaseType=" + DocBaseType);
            //
            MPeriodControl pc = new MPeriodControl(ctx, Client_ID, C_Period_ID, DocBaseType, trxName);
            if (pc.save()) {
                counter++;
                s_log.fine(pc.toString());
            } else
                s_log.warning("Not saved: " + pc);
        }
        rs.close();
        pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        s_log.log(Level.SEVERE, sql, e);
    }
    try {
        if (pstmt != null)
            pstmt.close();
        pstmt = null;
    } catch (Exception e) {
        pstmt = null;
    }
    if (sp != null)
        sp.addLog(0, null, new BigDecimal(counter), "@C_PeriodControl_ID@ @Created@");
    s_log.info("Inserted #" + counter);
}
Also used : ResultSet(java.sql.ResultSet) MPeriodControl(org.compiere.model.MPeriodControl) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Example 2 with MPeriodControl

use of org.compiere.model.MPeriodControl in project adempiere by adempiere.

the class PeriodControlStatus method doIt.

//	prepare
/**
	 * 	Process
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("C_PeriodControl_ID=" + p_C_PeriodControl_ID);
    MPeriodControl pc = new MPeriodControl(getCtx(), p_C_PeriodControl_ID, get_TrxName());
    if (pc.get_ID() == 0)
        throw new AdempiereUserError("@NotFound@  @C_PeriodControl_ID@=" + p_C_PeriodControl_ID);
    //	Permanently closed
    if (MPeriodControl.PERIODACTION_PermanentlyClosePeriod.equals(pc.getPeriodStatus()))
        throw new AdempiereUserError("@PeriodStatus@ = " + pc.getPeriodStatus());
    //	No Action
    if (MPeriodControl.PERIODACTION_NoAction.equals(pc.getPeriodAction()))
        return "@OK@";
    //	Open
    if (MPeriodControl.PERIODACTION_OpenPeriod.equals(pc.getPeriodAction()))
        pc.setPeriodStatus(MPeriodControl.PERIODSTATUS_Open);
    //	Close
    if (MPeriodControl.PERIODACTION_ClosePeriod.equals(pc.getPeriodAction()))
        pc.setPeriodStatus(MPeriodControl.PERIODSTATUS_Closed);
    //	Close Permanently
    if (MPeriodControl.PERIODACTION_PermanentlyClosePeriod.equals(pc.getPeriodAction()))
        pc.setPeriodStatus(MPeriodControl.PERIODSTATUS_PermanentlyClosed);
    pc.setPeriodAction(MPeriodControl.PERIODACTION_NoAction);
    //
    boolean ok = pc.save();
    //	Reset Cache
    CacheMgt.get().reset("C_PeriodControl", 0);
    CacheMgt.get().reset("C_Period", pc.getC_Period_ID());
    if (!ok)
        return "@Error@";
    return "@OK@";
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MPeriodControl(org.compiere.model.MPeriodControl)

Aggregations

MPeriodControl (org.compiere.model.MPeriodControl)2 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1