Search in sources :

Example 1 with MElementValue

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

the class TrialBalance method createBalanceLine.

//	doIt
/**
	 * 	Create Beginning Balance Line
	 */
private void createBalanceLine() {
    StringBuffer sql = new StringBuffer(s_insert);
    //	(AD_PInstance_ID, Fact_Acct_ID,
    sql.append("SELECT ").append(getAD_PInstance_ID()).append(",0,");
    //	AD_Client_ID, AD_Org_ID, Created,CreatedBy, Updated,UpdatedBy,
    sql.append(getAD_Client_ID()).append(",");
    if (p_AD_Org_ID == 0)
        sql.append("0");
    else
        sql.append(p_AD_Org_ID);
    sql.append(", SysDate,").append(getAD_User_ID()).append(",SysDate,").append(getAD_User_ID()).append(",");
    //	C_AcctSchema_ID, Account_ID, AccountValue, DateTrx, DateAcct, C_Period_ID,
    sql.append(p_C_AcctSchema_ID).append(",");
    if (p_Account_ID == 0)
        sql.append("null");
    else
        sql.append(p_Account_ID);
    if (p_AccountValue_From != null)
        sql.append(",").append(DB.TO_STRING(p_AccountValue_From));
    else if (p_AccountValue_To != null)
        sql.append(",' '");
    else
        sql.append(",null");
    // TimeUtil.addDays(p_DateAcct_From, -1);
    Timestamp balanceDay = p_DateAcct_From;
    sql.append(",null,").append(DB.TO_DATE(balanceDay, true)).append(",");
    if (p_C_Period_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_Period_ID);
    sql.append(",");
    //	AD_Table_ID, Record_ID, Line_ID,
    sql.append("null,null,null,");
    //	GL_Category_ID, GL_Budget_ID, C_Tax_ID, M_Locator_ID, PostingType,
    sql.append("null,null,null,null,'").append(p_PostingType).append("',");
    //	C_Currency_ID, AmtSourceDr, AmtSourceCr, AmtSourceBalance,
    sql.append("null,null,null,null,");
    //	AmtAcctDr, AmtAcctCr, AmtAcctBalance, C_UOM_ID, Qty,
    sql.append(" COALESCE(SUM(AmtAcctDr),0),COALESCE(SUM(AmtAcctCr),0)," + "COALESCE(SUM(AmtAcctDr),0)-COALESCE(SUM(AmtAcctCr),0)," + " null,COALESCE(SUM(Qty),0),");
    //	M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID,C_LocTo_ID,
    if (p_M_Product_ID == 0)
        sql.append("null");
    else
        sql.append(p_M_Product_ID);
    sql.append(",");
    if (p_C_BPartner_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_BPartner_ID);
    sql.append(",");
    if (p_AD_OrgTrx_ID == 0)
        sql.append("null");
    else
        sql.append(p_AD_OrgTrx_ID);
    sql.append(",");
    if (p_C_LocFrom_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_LocFrom_ID);
    sql.append(",");
    if (p_C_LocTo_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_LocTo_ID);
    sql.append(",");
    //	C_SalesRegion_ID, C_Project_ID, C_Campaign_ID, C_Activity_ID,
    if (p_C_SalesRegion_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_SalesRegion_ID);
    sql.append(",");
    if (p_C_Project_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_Project_ID);
    sql.append(",");
    if (p_C_Campaign_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_Campaign_ID);
    sql.append(",");
    if (p_C_Activity_ID == 0)
        sql.append("null");
    else
        sql.append(p_C_Activity_ID);
    sql.append(",");
    //	User1_ID, User2_ID, A_Asset_ID, Description)
    if (p_User1_ID == 0)
        sql.append("null");
    else
        sql.append(p_User1_ID);
    sql.append(",");
    if (p_User2_ID == 0)
        sql.append("null");
    else
        sql.append(p_User2_ID);
    sql.append(",");
    //	User3_ID, User4_ID, A_Asset_ID, Description)
    if (p_User3_ID == 0)
        sql.append("null");
    else
        sql.append(p_User3_ID);
    sql.append(",");
    if (p_User4_ID == 0)
        sql.append("null");
    else
        sql.append(p_User4_ID);
    sql.append(", null,null");
    //
    sql.append(" FROM Fact_Acct WHERE AD_Client_ID=").append(getAD_Client_ID()).append(" AND ").append(m_parameterWhere).append(" AND DateAcct < ").append(DB.TO_DATE(p_DateAcct_From, true));
    //	Start Beginning of Year
    if (p_Account_ID > 0) {
        m_acct = new MElementValue(getCtx(), p_Account_ID, get_TrxName());
        if (!m_acct.isBalanceSheet()) {
            MPeriod first = MPeriod.getFirstInYear(getCtx(), p_DateAcct_From, p_AD_Org_ID);
            if (first != null)
                sql.append(" AND DateAcct >= ").append(DB.TO_DATE(first.getStartDate(), true));
            else
                log.log(Level.SEVERE, "first period not found");
        }
    }
    //
    int no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no == 0)
        log.fine(sql.toString());
    log.fine("#" + no + " (Account_ID=" + p_Account_ID + ")");
}
Also used : MElementValue(org.compiere.model.MElementValue) MPeriod(org.compiere.model.MPeriod) Timestamp(java.sql.Timestamp)

Example 2 with MElementValue

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

the class FinStatement method createBalanceLine.

//	doIt
/**
	 * 	Create Beginning Balance Line
	 */
private void createBalanceLine() {
    StringBuffer sb = new StringBuffer("INSERT INTO T_ReportStatement " + "(AD_PInstance_ID, Fact_Acct_ID, LevelNo," + "DateAcct, Name, Description," + "AmtAcctDr, AmtAcctCr, Balance, Qty) ");
    sb.append("SELECT ").append(getAD_PInstance_ID()).append(",0,0,").append(DB.TO_DATE(p_DateAcct_From, true)).append(",").append(DB.TO_STRING(Msg.getMsg(Env.getCtx(), "BeginningBalance"))).append(",NULL," + "COALESCE(SUM(AmtAcctDr),0), COALESCE(SUM(AmtAcctCr),0), COALESCE(SUM(AmtAcctDr-AmtAcctCr),0), COALESCE(SUM(Qty),0) " + "FROM Fact_Acct " + "WHERE ").append(m_parameterWhere).append(" AND TRUNC(DateAcct, 'DD') < ").append(DB.TO_DATE(p_DateAcct_From));
    //	Start Beginning of Year
    if (p_Account_ID > 0) {
        m_acct = new MElementValue(getCtx(), p_Account_ID, get_TrxName());
        if (!m_acct.isBalanceSheet()) {
            MPeriod first = MPeriod.getFirstInYear(getCtx(), p_DateAcct_From, p_AD_Org_ID);
            if (first != null)
                sb.append(" AND TRUNC(DateAcct, 'DD') >= ").append(DB.TO_DATE(first.getStartDate()));
            else
                log.log(Level.SEVERE, "First period not found");
        }
    }
    //
    int no = DB.executeUpdate(sb.toString(), get_TrxName());
    log.fine("#" + no + " (Account_ID=" + p_Account_ID + ")");
    log.finest(sb.toString());
}
Also used : MElementValue(org.compiere.model.MElementValue) MPeriod(org.compiere.model.MPeriod)

Example 3 with MElementValue

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

the class ExpenseTypesFromAccounts method doIt.

@Override
protected String doIt() throws Exception {
    // Fetch price list
    MPriceList priceList = new MPriceList(getCtx(), m_priceListId, get_TrxName());
    // Get current client id from price list since I for some reason can't read it from
    // context.
    m_clientId = priceList.getAD_Client_ID();
    // Get active price list version
    MPriceListVersion pv = priceList.getPriceListVersion(null);
    if (pv == null)
        throw new Exception("Pricelist " + priceList.getName() + " has no default version.");
    MProduct product;
    // Read all existing applicable products into memory for quick comparison.
    List<MProduct> products = new Query(getCtx(), I_M_Product.Table_Name, "ProductType=?", get_TrxName()).setParameters(MProduct.PRODUCTTYPE_ExpenseType).list();
    Map<String, MProduct> productMap = new TreeMap<String, MProduct>();
    for (Iterator<MProduct> it = products.iterator(); it.hasNext(); ) {
        product = it.next();
        productMap.put(product.getValue(), product);
    }
    // Read all existing valid combinations comparison
    MAccount validComb;
    List<MAccount> validCombs = new Query(getCtx(), I_C_ValidCombination.Table_Name, "C_AcctSchema_ID=? and AD_Client_ID=? and AD_Org_ID=0", get_TrxName()).setParameters(m_acctSchemaId, m_clientId).list();
    Map<Integer, MAccount> validCombMap = new TreeMap<Integer, MAccount>();
    for (Iterator<MAccount> it = validCombs.iterator(); it.hasNext(); ) {
        validComb = it.next();
        validCombMap.put(validComb.getAccount_ID(), validComb);
    }
    // Read all accounttypes that fit the given criteria.
    List<MElementValue> result = new Query(getCtx(), I_C_ElementValue.Table_Name, "AccountType=? and isSummary='N' and Value>=? and Value<=? and AD_Client_ID=?", get_TrxName()).setParameters(MElementValue.ACCOUNTTYPE_Expense, m_startElement, m_endElement, m_clientId).list();
    MElementValue elem;
    MProductPrice priceRec;
    X_M_Product_Acct productAcct;
    String expenseItemValue;
    BigDecimal zero = Env.ZERO;
    int addCount = 0;
    int skipCount = 0;
    for (Iterator<MElementValue> it = result.iterator(); it.hasNext(); ) {
        elem = it.next();
        expenseItemValue = m_productValuePrefix + elem.getValue() + m_productValueSuffix;
        // See if a product with this key already exists
        product = productMap.get(expenseItemValue);
        if (product == null) {
            // Create a new product from the account element
            product = new MProduct(getCtx(), 0, get_TrxName());
            product.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
            product.setValue(expenseItemValue);
            product.setName(elem.getName());
            product.setDescription(elem.getDescription());
            product.setIsActive(true);
            product.setProductType(MProduct.PRODUCTTYPE_ExpenseType);
            product.setM_Product_Category_ID(m_productCategoryId);
            product.setC_UOM_ID(m_uomId);
            product.setC_TaxCategory_ID(m_taxCategoryId);
            product.setIsStocked(false);
            product.setIsPurchased(true);
            product.setIsSold(false);
            // Save the product
            product.saveEx(get_TrxName());
            // Add a zero product price to the price list so it shows up in the price list
            priceRec = new MProductPrice(getCtx(), pv.get_ID(), product.get_ID(), get_TrxName());
            priceRec.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
            priceRec.setPrices(zero, zero, zero);
            priceRec.saveEx(get_TrxName());
            // Set the revenue and expense accounting of the product to the given account element
            // Get the valid combination
            validComb = validCombMap.get(elem.getC_ElementValue_ID());
            if (validComb == null) {
                // Create new valid combination
                validComb = new MAccount(getCtx(), 0, get_TrxName());
                validComb.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
                validComb.setAD_Org_ID(0);
                validComb.setAlias(elem.getValue());
                validComb.setAccount_ID(elem.get_ID());
                validComb.setC_AcctSchema_ID(m_acctSchemaId);
                validComb.saveEx(get_TrxName());
            }
            // TODO: It might be needed to make the accounting more specific, but the purpose
            // of the process now is to create general accounts so this is intentional.
            productAcct = new Query(getCtx(), I_M_Product_Acct.Table_Name, "M_Product_ID=? and C_AcctSchema_ID=?", get_TrxName()).setParameters(product.get_ID(), m_acctSchemaId).first();
            productAcct.setP_Expense_Acct(validComb.get_ID());
            productAcct.setP_Revenue_Acct(validComb.get_ID());
            productAcct.saveEx(get_TrxName());
            addCount++;
        } else {
            skipCount++;
        }
    }
    String returnStr = addCount + " products added.";
    if (skipCount > 0)
        returnStr += " " + skipCount + " products skipped.";
    return (returnStr);
}
Also used : MElementValue(org.compiere.model.MElementValue) MProduct(org.compiere.model.MProduct) Query(org.compiere.model.Query) MAccount(org.compiere.model.MAccount) MPriceListVersion(org.compiere.model.MPriceListVersion) MPriceList(org.compiere.model.MPriceList) TreeMap(java.util.TreeMap) X_M_Product_Acct(org.compiere.model.X_M_Product_Acct) BigDecimal(java.math.BigDecimal) MProductPrice(org.compiere.model.MProductPrice)

Example 4 with MElementValue

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

the class Fact method checkAccounts.

//  balanceAccounting
/**
	 * 	Check Accounts of Fact Lines
	 *	@return true if success
	 */
public boolean checkAccounts() {
    //  no lines -> nothing to distribute
    if (m_lines.size() == 0)
        return true;
    //	For all fact lines
    for (int i = 0; i < m_lines.size(); i++) {
        FactLine line = (FactLine) m_lines.get(i);
        MAccount account = line.getAccount();
        if (account == null) {
            log.warning("No Account for " + line);
            return false;
        }
        MElementValue ev = account.getAccount();
        if (ev == null) {
            log.warning("No Element Value for " + account + ": " + line);
            return false;
        }
        if (ev.isSummary()) {
            log.warning("Cannot post to Summary Account " + ev + ": " + line);
            return false;
        }
        if (!ev.isActive()) {
            log.warning("Cannot post to Inactive Account " + ev + ": " + line);
            return false;
        }
    }
    return true;
}
Also used : MElementValue(org.compiere.model.MElementValue) MAccount(org.compiere.model.MAccount)

Example 5 with MElementValue

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

the class ImportAccount method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message
	 *  @throws Exception
	 */
protected String doIt() throws java.lang.Exception {
    StringBuffer sql = null;
    int no = 0;
    String clientCheck = " AND AD_Client_ID=" + m_AD_Client_ID;
    //	Delete Old Imported
    if (m_deleteOldImported) {
        sql = new StringBuffer("DELETE I_ElementValue " + "WHERE I_IsImported='Y'").append(clientCheck);
        no = DB.executeUpdate(sql.toString(), get_TrxName());
        log.fine("Delete Old Impored =" + no);
    }
    //	Set Client, Org, IsActive, Created/Updated
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET AD_Client_ID = COALESCE (AD_Client_ID, ").append(m_AD_Client_ID).append(")," + " AD_Org_ID = COALESCE (AD_Org_ID, 0)," + " IsActive = COALESCE (IsActive, 'Y')," + " Created = COALESCE (Created, SysDate)," + " CreatedBy = COALESCE (CreatedBy, 0)," + " Updated = COALESCE (Updated, SysDate)," + " UpdatedBy = COALESCE (UpdatedBy, 0)," + " I_ErrorMsg = ' '," + " Processed = 'N', " + " I_IsImported = 'N' " + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Reset=" + no);
    //	Set Element
    if (m_C_Element_ID != 0) {
        sql = new StringBuffer("UPDATE I_ElementValue " + "SET ElementName=(SELECT Name FROM C_Element WHERE C_Element_ID=").append(m_C_Element_ID).append(") " + "WHERE ElementName IS NULL AND C_Element_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
        no = DB.executeUpdate(sql.toString(), get_TrxName());
        log.fine("Set Element Default=" + no);
    }
    //
    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET C_Element_ID = (SELECT C_Element_ID FROM C_Element e" + " WHERE i.ElementName=e.Name AND i.AD_Client_ID=e.AD_Client_ID)" + "WHERE C_Element_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Element=" + no);
    //
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Element, ' " + "WHERE C_Element_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Invalid Element=" + no);
    //	No Name, Value
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Name, ' " + "WHERE (Value IS NULL OR Name IS NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Invalid Name=" + no);
    //	Set Column
    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET AD_Column_ID = (SELECT AD_Column_ID FROM AD_Column c" + " WHERE UPPER(i.Default_Account)=UPPER(c.ColumnName)" + " AND c.AD_Table_ID IN (315,266) AND AD_Reference_ID=25) " + "WHERE Default_Account IS NOT NULL AND AD_Column_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Column=" + no);
    //
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Column, ' " + "WHERE AD_Column_ID IS NULL AND Default_Account IS NOT NULL" + //	ignore default account
    " AND UPPER(Default_Account)<>'DEFAULT_ACCT'" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Invalid Column=" + no);
    //	Set Post* Defaults (ignore errors)
    String[] yColumns = new String[] { "PostActual", "PostBudget", "PostStatistical", "PostEncumbrance" };
    for (int i = 0; i < yColumns.length; i++) {
        sql = new StringBuffer("UPDATE I_ElementValue SET ").append(yColumns[i]).append("='Y' WHERE ").append(yColumns[i]).append(" IS NULL OR ").append(yColumns[i]).append(" NOT IN ('Y','N')" + " AND I_IsImported<>'Y'").append(clientCheck);
        no = DB.executeUpdate(sql.toString(), get_TrxName());
        log.fine("Set " + yColumns[i] + " Default=" + no);
    }
    //	Summary
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET IsSummary='N' " + "WHERE IsSummary IS NULL OR IsSummary NOT IN ('Y','N')" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set IsSummary Default=" + no);
    //	Doc Controlled
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET IsDocControlled = CASE WHEN AD_Column_ID IS NOT NULL THEN 'Y' ELSE 'N' END " + "WHERE IsDocControlled IS NULL OR IsDocControlled NOT IN ('Y','N')" + " AND I_IsImported='N'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set IsDocumentControlled Default=" + no);
    //	Check Account Type A (E) L M O R
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET AccountType='E' " + "WHERE AccountType IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set AccountType Default=" + no);
    //
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid AccountType, ' " + "WHERE AccountType NOT IN ('A','E','L','M','O','R')" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Invalid AccountType=" + no);
    //	Check Account Sign (N) C B
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET AccountSign='N' " + "WHERE AccountSign IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set AccountSign Default=" + no);
    //
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid AccountSign, ' " + "WHERE AccountSign NOT IN ('N','C','D')" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Invalid AccountSign=" + no);
    //	No Value
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Key, ' " + "WHERE (Value IS NULL OR Value='')" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Invalid Key=" + no);
    //	****	Update ElementValue from existing
    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET C_ElementValue_ID=(SELECT C_ElementValue_ID FROM C_ElementValue ev" + " INNER JOIN C_Element e ON (ev.C_Element_ID=e.C_Element_ID)" + " WHERE i.C_Element_ID=e.C_Element_ID AND i.AD_Client_ID=e.AD_Client_ID" + " AND i.Value=ev.Value) " + "WHERE C_ElementValue_ID IS NULL" + " AND I_IsImported='N'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Found ElementValue=" + no);
    //  update Charge
    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET C_Charge_ID = (SELECT C_Charge_ID FROM C_Charge c" + " WHERE i.ChargeName=c.Name AND i.AD_Client_ID=c.AD_Client_ID)" + "WHERE C_Charge_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Charge=" + no);
    //  update Tax Category
    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET C_TaxCategory_ID = (SELECT C_TaxCategory_ID FROM C_TaxCategory c" + " WHERE i.TaxCategoryName=c.Name AND i.AD_Client_ID=c.AD_Client_ID)" + "WHERE C_TaxCategory_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Tax Category=" + no);
    commitEx();
    //	-------------------------------------------------------------------
    int noInsert = 0;
    int noUpdate = 0;
    //	Go through Records
    sql = new StringBuffer("SELECT * " + "FROM I_ElementValue " + "WHERE I_IsImported='N'").append(clientCheck).append(" ORDER BY I_ElementValue_ID");
    try {
        PreparedStatement pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            X_I_ElementValue impEV = new X_I_ElementValue(getCtx(), rs, get_TrxName());
            int C_ElementValue_ID = impEV.getC_ElementValue_ID();
            int I_ElementValue_ID = impEV.getI_ElementValue_ID();
            //	****	Create/Update ElementValue
            if (//	New
            C_ElementValue_ID == 0) {
                MElementValue ev = new MElementValue(impEV);
                if (ev.save()) {
                    noInsert++;
                    if (!ev.isSummary())
                        updateCharge(impEV, ev);
                    impEV.setC_ElementValue_ID(ev.getC_ElementValue_ID());
                    impEV.setI_IsImported(true);
                    impEV.saveEx();
                } else {
                    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert ElementValue ")).append("WHERE I_ElementValue_ID=").append(I_ElementValue_ID);
                    DB.executeUpdate(sql.toString(), get_TrxName());
                }
            } else //	Update existing
            {
                MElementValue ev = new MElementValue(getCtx(), C_ElementValue_ID, get_TrxName());
                if (ev.get_ID() != C_ElementValue_ID) {
                }
                ev.set(impEV);
                if (ev.save()) {
                    noUpdate++;
                    if (!ev.isSummary())
                        updateCharge(impEV, ev);
                    impEV.setI_IsImported(true);
                    impEV.saveEx();
                } else {
                    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Update ElementValue")).append("WHERE I_ElementValue_ID=").append(I_ElementValue_ID);
                    DB.executeUpdate(sql.toString(), get_TrxName());
                }
            }
        }
        //	for all I_ElementValue
        rs.close();
        pstmt.close();
    } catch (SQLException e) {
        throw new Exception("create", e);
    }
    //	Set Error to indicator to not imported
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_IsImported='N', Updated=SysDate " + "WHERE I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    addLog(0, null, new BigDecimal(no), "@Errors@");
    addLog(0, null, new BigDecimal(noInsert), "@C_ElementValue_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noUpdate), "@C_ElementValue_ID@: @Updated@");
    commitEx();
    //	*****	Set Parent
    sql = new StringBuffer("UPDATE I_ElementValue i " + "SET ParentElementValue_ID=(SELECT C_ElementValue_ID" + " FROM C_ElementValue ev WHERE i.C_Element_ID=ev.C_Element_ID" + " AND i.ParentValue=ev.Value AND i.AD_Client_ID=ev.AD_Client_ID) " + "WHERE ParentElementValue_ID IS NULL" + " AND I_IsImported='Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Found Parent ElementValue=" + no);
    //
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET I_ErrorMsg=I_ErrorMsg||'Info=ParentNotFound, ' " + "WHERE ParentElementValue_ID IS NULL AND ParentValue IS NOT NULL" + " AND I_IsImported='Y' AND Processed='N'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.config("Not Found Parent ElementValue=" + no);
    //
    sql = new StringBuffer("SELECT i.ParentElementValue_ID, i.I_ElementValue_ID," + " e.AD_Tree_ID, i.C_ElementValue_ID, i.Value||'-'||i.Name AS Info " + "FROM I_ElementValue i" + " INNER JOIN C_Element e ON (i.C_Element_ID=e.C_Element_ID) " + "WHERE i.C_ElementValue_ID IS NOT NULL AND e.AD_Tree_ID IS NOT NULL" + " AND i.ParentElementValue_ID IS NOT NULL" + " AND i.I_IsImported='Y' AND Processed='N' AND i.AD_Client_ID=").append(m_AD_Client_ID);
    int noParentUpdate = 0;
    try {
        PreparedStatement pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        ResultSet rs = pstmt.executeQuery();
        //
        String updateSQL = "UPDATE AD_TreeNode SET Parent_ID=?, SeqNo=? " + "WHERE AD_Tree_ID=? AND Node_ID=?";
        //begin e-evolution vpj-cd 15 nov 2005 PostgreSQL
        //PreparedStatement updateStmt = DB.prepareStatement(updateSQL, get_TrxName());
        PreparedStatement updateStmt = DB.prepareStatement(updateSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, get_TrxName());
        //
        while (rs.next()) {
            //	Parent
            updateStmt.setInt(1, rs.getInt(1));
            //	SeqNo (assume sequenec in import is the same)
            updateStmt.setInt(2, rs.getInt(2));
            //	Tree
            updateStmt.setInt(3, rs.getInt(3));
            //	Node
            updateStmt.setInt(4, rs.getInt(4));
            try {
                no = updateStmt.executeUpdate();
                noParentUpdate += no;
            } catch (SQLException ex) {
                log.log(Level.SEVERE, "(ParentUpdate)", ex);
                no = 0;
            }
            if (no == 0)
                log.info("Parent not found for " + rs.getString(5));
        }
        rs.close();
        pstmt.close();
    } catch (SQLException e) {
        log.log(Level.SEVERE, "(ParentUpdateLoop) " + sql.toString(), e);
    }
    addLog(0, null, new BigDecimal(noParentUpdate), "@ParentElementValue_ID@: @Updated@");
    commitEx();
    //	Reset Processing Flag
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET Processing='-'" + "WHERE I_IsImported='Y' AND Processed='N' AND Processing='Y'" + " AND C_ElementValue_ID IS NOT NULL").append(clientCheck);
    if (m_updateDefaultAccounts)
        sql.append(" AND AD_Column_ID IS NULL");
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Reset Processing Flag=" + no);
    if (m_updateDefaultAccounts)
        updateDefaults(clientCheck);
    //	Update Description
    sql = new StringBuffer("SELECT * FROM C_ValidCombination vc " + "WHERE EXISTS (SELECT * FROM I_ElementValue i " + "WHERE vc.Account_ID=i.C_ElementValue_ID)");
    //	Done
    sql = new StringBuffer("UPDATE I_ElementValue " + "SET Processing='N', Processed='Y'" + "WHERE I_IsImported='Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Processed=" + no);
    return "";
}
Also used : MElementValue(org.compiere.model.MElementValue) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) X_I_ElementValue(org.compiere.model.X_I_ElementValue) SQLException(java.sql.SQLException) BigDecimal(java.math.BigDecimal)

Aggregations

MElementValue (org.compiere.model.MElementValue)6 BigDecimal (java.math.BigDecimal)2 MAccount (org.compiere.model.MAccount)2 MPeriod (org.compiere.model.MPeriod)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 TreeMap (java.util.TreeMap)1 MPriceList (org.compiere.model.MPriceList)1 MPriceListVersion (org.compiere.model.MPriceListVersion)1 MProduct (org.compiere.model.MProduct)1 MProductPrice (org.compiere.model.MProductPrice)1 Query (org.compiere.model.Query)1 X_I_ElementValue (org.compiere.model.X_I_ElementValue)1 X_M_Product_Acct (org.compiere.model.X_M_Product_Acct)1