Search in sources :

Example 1 with MPPForecastDefinitionLine

use of org.eevolution.model.MPPForecastDefinitionLine in project adempiere by adempiere.

the class ForecastRunCreate method generateForecastRunMaster.

/**
	 * Generate Forecast Master based on sales history
	 * 
	 * @param run
	 *            Forecast Simulation
	 * @param fd
	 *            Forecast Definition
	 * @param start
	 *            Start Operational Period
	 * @param end
	 *            End Operational Period
	 * @return no records processed
	 */
private int generateForecastRunMaster(MPPForecastRun run, MPPForecastDefinition fd, MPPPeriod start, MPPPeriod end) {
    int count = 0;
    for (MPPForecastDefinitionLine fdl : fd.getLines(true)) {
        final StringBuffer select = new StringBuffer("SELECT DISTINCT sh.M_Product_ID , sh.M_Warehouse_ID FROM C_SalesHistory sh LEFT JOIN  PP_ForecastRunMaster m ON (m.M_Product_ID=sh.M_Product_ID AND ");
        select.append(MPPForecastRunMaster.COLUMNNAME_PP_ForecastRun_ID);
        select.append("=").append(run.get_ID()).append(" ) WHERE  ");
        select.append(fdl.getSQlWhere(run.getM_WarehouseSource_ID(), "sh"));
        select.append(MSalesHistory.COLUMNNAME_DateInvoiced);
        select.append(" BETWEEN ");
        select.append(DB.TO_DATE(start.getStartDate()));
        select.append(" AND ");
        select.append(DB.TO_DATE(end.getEndDate()));
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = DB.prepareStatement(select.toString(), get_TrxName());
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count++;
                int M_Product_ID = rs.getInt("M_Product_ID");
                MPPForecastRunMaster master = MPPForecastRunMaster.getByProduct(getCtx(), run.getPP_ForecastRun_ID(), M_Product_ID, get_TrxName());
                if (master != null)
                    continue;
                master = new MPPForecastRunMaster(getCtx(), 0, get_TrxName());
                master.setAD_Org_ID(run.getAD_Org_ID());
                master.setPP_ForecastRun_ID(run.get_ID());
                master.setPP_ForecastDefinitionLine_ID(fdl.getPP_ForecastDefinitionLine_ID());
                master.setM_Product_ID(M_Product_ID);
                master.setM_Warehouse_ID(run.getM_Warehouse_ID());
                master.setFactorAlpha(fdl.getFactorAlpha());
                master.setFactorGamma(fdl.getFactorGamma());
                master.setFactorBeta(fdl.getFactorBeta());
                master.setFactorMultiplier(fdl.getFactorMultiplier());
                master.setFactorScale(fdl.getFactorScale());
                master.setFactorUser(fdl.getFactorUser());
                master.saveEx();
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, select.toString(), e);
            getProcessInfo().addLog(getProcessInfo().getAD_PInstance_ID(), null, null, e.getLocalizedMessage());
        } finally {
            DB.close(rs, pstmt);
            rs = null;
            pstmt = null;
        }
    }
    return count;
}
Also used : MPPForecastDefinitionLine(org.eevolution.model.MPPForecastDefinitionLine) ResultSet(java.sql.ResultSet) MPPForecastRunMaster(org.eevolution.model.MPPForecastRunMaster) PreparedStatement(java.sql.PreparedStatement)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 MPPForecastDefinitionLine (org.eevolution.model.MPPForecastDefinitionLine)1 MPPForecastRunMaster (org.eevolution.model.MPPForecastRunMaster)1