Search in sources :

Example 1 with X_M_Product_Acct

use of org.compiere.model.X_M_Product_Acct 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)

Aggregations

BigDecimal (java.math.BigDecimal)1 TreeMap (java.util.TreeMap)1 MAccount (org.compiere.model.MAccount)1 MElementValue (org.compiere.model.MElementValue)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_M_Product_Acct (org.compiere.model.X_M_Product_Acct)1