Search in sources :

Example 76 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class MDepreciationMethod method invoke.

/**
	 * Calculate adjustment
	 * @return adjustment to be applied in the specified period
	 */
public BigDecimal invoke(int A_Asset_ID, BigDecimal A_Asset_Adjustment, int A_PeriodNo, String PostingType, int A_Asset_Acct_ID) {
    String depreciationType = getDepreciationType();
    BigDecimal retValue = null;
    if (CLogMgt.isLevelFine())
        log.fine("Entering: DepreciationMethodType=" + depreciationType + ", A_Asset_ID=" + A_Asset_ID + ", A_Asset_Adjustment=" + A_Asset_Adjustment + ", A_PeriodNo=" + A_PeriodNo + ", PostingType=" + PostingType + ", A_Asset_Acct_ID=" + A_Asset_Acct_ID);
    if (depreciationType.equalsIgnoreCase("MDI")) {
        retValue = apply_MDI(A_Asset_ID, A_Asset_Adjustment, A_PeriodNo, PostingType, A_Asset_Acct_ID);
    } else if (depreciationType.equalsIgnoreCase("YDI")) {
        retValue = apply_YDI(A_Asset_ID, A_Asset_Adjustment, A_PeriodNo, PostingType, A_Asset_Acct_ID);
    } else if (depreciationType.equalsIgnoreCase("LDI")) {
        retValue = apply_LDI(A_Asset_ID, A_Asset_Adjustment, A_PeriodNo, PostingType, A_Asset_Acct_ID);
    } else {
        String sql = "{ ? = call " + depreciationType + "(?, ?, ?, ?, ?) }";
        CallableStatement cs = null;
        try {
            cs = DB.prepareCall(sql);
            cs.registerOutParameter(1, java.sql.Types.DECIMAL);
            cs.setInt(2, A_Asset_ID);
            cs.setBigDecimal(3, A_Asset_Adjustment);
            cs.setInt(4, A_PeriodNo);
            cs.setString(5, PostingType);
            cs.setInt(6, A_Asset_Acct_ID);
            cs.execute();
            retValue = cs.getBigDecimal(1);
            cs.close();
        } catch (SQLException e) {
            throw new DBException(e);
        } finally {
            DB.close(cs);
            cs = null;
        }
    }
    //
    if (retValue == null) {
        retValue = BigDecimal.ZERO;
    }
    //
    if (CLogMgt.isLevelFine())
        log.fine("Leaving: retValue=" + retValue);
    return retValue;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) BigDecimal(java.math.BigDecimal)

Example 77 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class CashFlow method doIt.

// prepare
protected String doIt() throws Exception {
    log.info("DueDate=" + p_DueDate + ", C_Currency_ID=" + p_C_Currency_ID + ", C_BP_Group_ID=" + p_C_BP_Group_ID + ", C_BPartner_ID=" + p_C_BPartner_ID);
    //
    final ArrayList<Object> params = new ArrayList<Object>();
    StringBuffer sql = new StringBuffer();
    sql.append(getSqlSelect("t", params));
    //
    String whereClause = getSqlWhereClause("t", params);
    if (!Util.isEmpty(whereClause, true)) {
        sql.append(" WHERE ").append(whereClause);
    }
    //
    sql.append(" ORDER BY ").append(getSqlSelectOrderBy("t", params));
    //
    // int counter = 0;
    //
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        DB.setParameters(pstmt, params);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            addLine(rs);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql.toString());
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //
    saveRow();
    //
    return "";
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 78 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class MRP method runMRP.

/**************************************************************************
	 *  Calculate plan
	 *  @param AD_Client_ID Client ID
	 *  @param AD_Org_ID Organization ID
	 *  @param M_Warehuse_ID Warehouse ID
	 * @throws SQLException 
	 */
protected String runMRP(int AD_Client_ID, int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID) throws SQLException {
    Trx.run(new TrxRunnable() {

        private int AD_Client_ID, AD_Org_ID, S_Resource_ID, M_Warehouse_ID;

        public TrxRunnable setParameters(int AD_Client_ID, int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID) {
            this.AD_Client_ID = AD_Client_ID;
            this.AD_Org_ID = AD_Org_ID;
            this.S_Resource_ID = S_Resource_ID;
            this.M_Warehouse_ID = M_Warehouse_ID;
            return this;
        }

        public void run(String trxName) {
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                MProduct product = null;
                int BeforePP_MRP_ID = 0;
                Timestamp BeforeDateStartSchedule = null;
                Timestamp POQDateStartSchedule = null;
                int lowlevel = MPPMRP.getMaxLowLevel(getCtx(), trxName);
                log.info("Low Level Is :" + lowlevel);
                // Calculate MRP for all levels
                for (int level = 0; level <= lowlevel; level++) {
                    log.info("Current Level Is :" + level);
                    StringBuilder sql = new StringBuilder();
                    sql.append("SELECT mrp.M_Product_ID, mrp.LowLevel, mrp.Qty, mrp.DatePromised").append(",mrp.TypeMRP, mrp.OrderType, mrp.DateOrdered, mrp.M_Warehouse_ID").append(",mrp.PP_MRP_ID, mrp.DateStartSchedule, mrp.DateFinishSchedule").append(" FROM RV_PP_MRP mrp WHERE 1=1 ").append(getSQLWhere("mrp", AD_Client_ID, AD_Org_ID, M_Warehouse_ID, null, null, level, MPPMRP.TYPEMRP_Demand, Planning_Horizon)).append(" ORDER BY  mrp.M_Product_ID , mrp.DatePromised");
                    pstmt = DB.prepareStatement(sql.toString(), trxName);
                    DB.setParameters(pstmt, parameters);
                    rs = pstmt.executeQuery();
                    log.info("Records " + rs.getFetchSize() + " to process for Low Code:" + level);
                    while (rs.next()) {
                        final int PP_MRP_ID = rs.getInt(MPPMRP.COLUMNNAME_PP_MRP_ID);
                        final String TypeMRP = rs.getString(MPPMRP.COLUMNNAME_TypeMRP);
                        final String OrderType = rs.getString(MPPMRP.COLUMNNAME_OrderType);
                        final Timestamp DatePromised = rs.getTimestamp(MPPMRP.COLUMNNAME_DateStartSchedule);
                        final BigDecimal Qty = rs.getBigDecimal(MPPMRP.COLUMNNAME_Qty);
                        final int M_Product_ID = rs.getInt(MPPMRP.COLUMNNAME_M_Product_ID);
                        // if demand is forecast and promised date less than or equal to today, ignore this QtyGrossReq
                        if (MPPMRP.TYPEMRP_Demand.equals(TypeMRP) && MPPMRP.ORDERTYPE_Forecast.equals(OrderType) && DatePromised.compareTo(getToday()) <= 0) {
                            continue;
                        }
                        // New Product
                        if (product == null || product.get_ID() != M_Product_ID) {
                            // If exist QtyGrossReqs of last Demand verify/calculate plan
                            if (QtyGrossReqs.signum() != 0) {
                                if (product == null) {
                                    throw new IllegalStateException("MRP Internal Error: QtyGrossReqs=" + QtyGrossReqs + " and we do not have previous demand defined");
                                }
                                if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy()) && POQDateStartSchedule.compareTo(Planning_Horizon) < 0) {
                                    BeforeDateStartSchedule = POQDateStartSchedule;
                                    calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
                                } else if (X_PP_Product_Planning.ORDER_POLICY_Lot_For_Lot.equals(m_product_planning.getOrder_Policy()) && BeforeDateStartSchedule.compareTo(Planning_Horizon) <= 0) {
                                    // TODO: Q: when we have this situation because on LFL we balance the Demand imediately
                                    //		so we do not cumullate it?
                                    calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
                                }
                                // Discard QtyGrossReqs because:
                                // * was already balanced by calculatePlan
                                // * is out of Planning Horizon
                                QtyGrossReqs = Env.ZERO;
                            }
                            //Setting MRP Change net Update out the model validator and out transaction
                            if (m_product_planning != null)
                                MPPMRP.setIsRequired(m_product_planning, MPPProductPlanning.COLUMNNAME_IsRequiredMRP, false, trxName);
                            // Load Product & define Product Data Planning
                            product = MProduct.get(getCtx(), M_Product_ID);
                            log.info("Calculte Plan to this Product:" + product);
                            setProduct(AD_Client_ID, AD_Org_ID, S_Resource_ID, M_Warehouse_ID, product, PP_MRP_ID, trxName);
                            // If No Product Planning found, go to next MRP record 
                            if (m_product_planning == null)
                                continue;
                            if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy())) {
                                POQDateStartSchedule = null;
                            }
                        }
                        // new product
                        demands.put(PP_MRP_ID, Qty);
                        // If No Product Planning found, go to next MRP record 
                        if (m_product_planning == null)
                            continue;
                        int daysPOQ = m_product_planning.getOrder_Period().intValueExact() - 1;
                        //first DatePromised.compareTo for ORDER_POLICY_PeriodOrderQuantity
                        if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy()) && (DatePromisedTo != null && DatePromised.compareTo(DatePromisedTo) > 0)) {
                            calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, PP_MRP_ID, product, DatePromisedFrom, trxName);
                            DatePromisedFrom = DatePromised;
                            DatePromisedTo = TimeUtil.addDays(DatePromised, daysPOQ < 0 ? 0 : daysPOQ);
                            POQDateStartSchedule = DatePromised;
                        } else if (POQDateStartSchedule == null) {
                            DatePromisedFrom = DatePromised;
                            DatePromisedTo = TimeUtil.addDays(DatePromised, daysPOQ < 0 ? 0 : daysPOQ);
                            POQDateStartSchedule = DatePromised;
                        }
                        //Indicates that a demand order is past due.
                        if (DatePromised.compareTo(getToday()) < 0) {
                            String comment = Msg.translate(getCtx(), MPPOrder.COLUMNNAME_DatePromised) + " : " + DatePromised;
                            createMRPNote("MRP-150", AD_Org_ID, PP_MRP_ID, product, MPPMRP.getDocumentNo(PP_MRP_ID), Qty, comment, trxName);
                        }
                        BeforePP_MRP_ID = PP_MRP_ID;
                        if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy())) {
                            // Verify if is DatePromised < DatePromisedTo then Accumulation QtyGrossReqs 
                            if (DatePromisedTo != null && DatePromised.compareTo(DatePromisedTo) <= 0) {
                                QtyGrossReqs = QtyGrossReqs.add(Qty);
                                log.info("Accumulation   QtyGrossReqs:" + QtyGrossReqs);
                                log.info("DatePromised:" + DatePromised);
                                log.info("DatePromisedTo:" + DatePromisedTo);
                                Trx.get(trxName, true).commit(true);
                                continue;
                            }
                        } else // If  Order_Policy = LoteForLote then always create new range for next period and put QtyGrossReqs          
                        if (X_PP_Product_Planning.ORDER_POLICY_Lot_For_Lot.equals(m_product_planning.getOrder_Policy())) {
                            QtyGrossReqs = QtyGrossReqs.add(Qty);
                            BeforeDateStartSchedule = DatePromised;
                            calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, PP_MRP_ID, product, BeforeDateStartSchedule, trxName);
                            Trx.get(trxName, true).commit(true);
                            continue;
                        }
                    }
                    // If exist QtyGrossReq of last Demand after finish while verify plan
                    if (QtyGrossReqs.signum() != 0 && product != null) {
                        if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy()) && POQDateStartSchedule.compareTo(Planning_Horizon) < 0) {
                            BeforeDateStartSchedule = POQDateStartSchedule;
                            calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
                        } else if (X_PP_Product_Planning.ORDER_POLICY_Lot_For_Lot.equals(m_product_planning.getOrder_Policy()) && BeforeDateStartSchedule.compareTo(Planning_Horizon) <= 0) {
                            calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
                        }
                    } else if (product != null) {
                        //Create Action Notice if exist supply
                        getNetRequirements(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, product, null, trxName);
                    }
                    createMRPPegging(trxName);
                    Trx.get(trxName, true).commit(true);
                    DB.close(rs, pstmt);
                }
            // end for
            }// try
             catch (SQLException ex) {
                throw new DBException(ex);
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
        }
    }.setParameters(AD_Client_ID, AD_Org_ID, S_Resource_ID, M_Warehouse_ID));
    return "ok";
}
Also used : DBException(org.adempiere.exceptions.DBException) MProduct(org.compiere.model.MProduct) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) POResultSet(org.compiere.model.POResultSet) TrxRunnable(org.compiere.util.TrxRunnable) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal)

Example 79 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class ReportStarter method getReportData.

/**
     * @author rlemeill
     * @param ProcessInfo
     * @return ReportData or null if no data found
     */
public ReportData getReportData(ProcessInfo pi, String trxName) {
    log.info("");
    String sql = "SELECT pr.JasperReport, pr.IsDirectPrint " + "FROM AD_Process pr, AD_PInstance pi " + "WHERE pr.AD_Process_ID = pi.AD_Process_ID " + " AND pi.AD_PInstance_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, trxName);
        pstmt.setInt(1, pi.getAD_PInstance_ID());
        rs = pstmt.executeQuery();
        String path = null;
        boolean directPrint = false;
        boolean isPrintPreview = pi.isPrintPreview();
        if (rs.next()) {
            path = rs.getString(1);
            if ("Y".equalsIgnoreCase(rs.getString(2)) && !Ini.isPropertyBool(Ini.P_PRINTPREVIEW) && !isPrintPreview)
                directPrint = true;
        } else {
            log.severe("data not found; sql = " + sql);
            return null;
        }
        return new ReportData(path, directPrint);
    } catch (SQLException e) {
        throw new DBException(e, sql);
    //        	log.severe("sql = "+sql+"; e.getMessage() = "+ e.getMessage());
    //        	return null;
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 80 with DBException

use of org.adempiere.exceptions.DBException in project adempiere by adempiere.

the class MInvoiceTax method calculateTaxFromLines.

//	getTax
/**************************************************************************
	 * 	Calculate/Set Tax Base Amt from Invoice Lines
	 * 	@return true if tax calculated
	 */
public boolean calculateTaxFromLines() {
    BigDecimal taxBaseAmt = Env.ZERO;
    BigDecimal taxAmt = Env.ZERO;
    //
    boolean documentLevel = getTax().isDocumentLevel();
    MTax tax = getTax();
    //
    String sql = "SELECT il.LineNetAmt, COALESCE(il.TaxAmt,0), i.IsSOTrx " + "FROM C_InvoiceLine il" + " INNER JOIN C_Invoice i ON (il.C_Invoice_ID=i.C_Invoice_ID) " + "WHERE il.C_Invoice_ID=? AND il.C_Tax_ID=?";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, get_TrxName());
        pstmt.setInt(1, getC_Invoice_ID());
        pstmt.setInt(2, getC_Tax_ID());
        rs = pstmt.executeQuery();
        while (rs.next()) {
            //	BaseAmt
            BigDecimal baseAmt = rs.getBigDecimal(1);
            taxBaseAmt = taxBaseAmt.add(baseAmt);
            //	TaxAmt
            BigDecimal amt = rs.getBigDecimal(2);
            if (amt == null)
                amt = Env.ZERO;
            boolean isSOTrx = "Y".equals(rs.getString(3));
            // on line level taxes
            if (//	manually entered
            !documentLevel && amt.signum() != 0)
                ;
            else if (documentLevel || baseAmt.signum() == 0)
                amt = Env.ZERO;
            else
                // calculate line tax
                amt = tax.calculateTax(baseAmt, isTaxIncluded(), getPrecision());
            //
            taxAmt = taxAmt.add(amt);
        }
    } catch (SQLException e) {
        throw new DBException(e, sql);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    //	Calculate Tax
    if (documentLevel || taxAmt.signum() == 0)
        taxAmt = tax.calculateTax(taxBaseAmt, isTaxIncluded(), getPrecision());
    setTaxAmt(taxAmt);
    //	Set Base
    if (isTaxIncluded())
        setTaxBaseAmt(taxBaseAmt.subtract(taxAmt));
    else
        setTaxBaseAmt(taxBaseAmt);
    return true;
}
Also used : DBException(org.adempiere.exceptions.DBException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BigDecimal(java.math.BigDecimal)

Aggregations

DBException (org.adempiere.exceptions.DBException)89 SQLException (java.sql.SQLException)82 PreparedStatement (java.sql.PreparedStatement)75 ResultSet (java.sql.ResultSet)75 BigDecimal (java.math.BigDecimal)27 ArrayList (java.util.ArrayList)23 Timestamp (java.sql.Timestamp)15 POResultSet (org.compiere.model.POResultSet)8 AdempiereException (org.adempiere.exceptions.AdempiereException)6 KeyNamePair (org.compiere.util.KeyNamePair)5 Connection (java.sql.Connection)4 Savepoint (java.sql.Savepoint)4 MProduct (org.compiere.model.MProduct)4 Trx (org.compiere.util.Trx)4 Date (java.util.Date)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IDColumn (org.compiere.minigrid.IDColumn)2 MLocator (org.compiere.model.MLocator)2 MTable (org.compiere.model.MTable)2 MUOM (org.compiere.model.MUOM)2