Search in sources :

Example 1 with MPriceList

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

the class ImportPriceList method doIt.

//	prepare
/**
	 *  Perform process.
	 *  @return Message
	 *  @throws Exception
	 */
protected String doIt() throws Exception {
    StringBuffer sql = null;
    int no = 0;
    String clientCheck = " AND AD_Client_ID=" + m_AD_Client_ID;
    int m_discountschema_id = DB.getSQLValue(get_TrxName(), "SELECT MIN(M_DiscountSchema_ID) FROM M_DiscountSchema WHERE DiscountType='P' AND IsActive='Y' AND AD_Client_ID=?", m_AD_Client_ID);
    if (m_discountschema_id <= 0)
        throw new AdempiereUserError("Price List Schema not configured");
    //	Delete Old Imported
    if (m_deleteOldImported) {
        sql = new StringBuffer("DELETE I_PriceList " + "WHERE I_IsImported='Y'").append(clientCheck);
        no = DB.executeUpdate(sql.toString(), get_TrxName());
        log.info("Delete Old Impored =" + no);
    }
    //	Set Client, Org, IsActive, Created/Updated, EnforcePriceLimit, IsSOPriceList, IsTaxIncluded, PricePrecision
    sql = new StringBuffer("UPDATE I_PriceList " + "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)," + " EnforcePriceLimit = COALESCE (EnforcePriceLimit, 'N')," + " IsSOPriceList = COALESCE (IsSOPriceList, 'N')," + " IsTaxIncluded = COALESCE (IsTaxIncluded, 'N')," + " PricePrecision = COALESCE (PricePrecision, 2)," + " I_ErrorMsg = ' '," + " I_IsImported = 'N' " + "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("Reset=" + no);
    //	Set Optional BPartner
    sql = new StringBuffer("UPDATE I_PriceList " + "SET C_BPartner_ID=(SELECT C_BPartner_ID FROM C_BPartner p" + " WHERE I_PriceList.BPartner_Value=p.Value AND I_PriceList.AD_Client_ID=p.AD_Client_ID) " + "WHERE C_BPartner_ID IS NULL AND BPartner_Value IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("BPartner=" + no);
    //
    sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid BPartner,' " + "WHERE C_BPartner_ID IS NULL AND BPartner_Value IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid BPartner=" + no);
    //	Product
    sql = new StringBuffer("UPDATE I_PriceList " + "SET M_Product_ID=(SELECT MAX(M_Product_ID) FROM M_Product p" + " WHERE I_PriceList.ProductValue=p.Value AND I_PriceList.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_Product_ID IS NULL AND ProductValue IS NOT NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Product from Value=" + no);
    sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Product, ' " + "WHERE M_Product_ID IS NULL AND (ProductValue IS NOT NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Product=" + no);
    //	****	Find Price List
    //	Name
    sql = new StringBuffer("UPDATE I_PriceList " + "SET M_PriceList_ID=(SELECT M_PriceList_ID FROM M_PriceList p" + " WHERE I_PriceList.Name=p.Name AND I_PriceList.AD_Client_ID=p.AD_Client_ID) " + "WHERE M_PriceList_ID IS NULL" + " AND I_IsImported='N'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("Price List Existing Value=" + no);
    //	****	Find Price List Version
    //	List Name (ID) + ValidFrom
    sql = new StringBuffer("UPDATE I_PriceList " + "SET M_PriceList_Version_ID=(SELECT M_PriceList_Version_ID FROM M_PriceList_Version p" + " WHERE I_PriceList.ValidFrom=p.ValidFrom AND I_PriceList.M_PriceList_ID=p.M_PriceList_ID) " + "WHERE M_PriceList_ID IS NOT NULL AND M_PriceList_Version_ID IS NULL" + " AND I_IsImported='N'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("Price List Version Existing Value=" + no);
    /* UOM For Future USE
		//	Set UOM (System/own)
		sql = new StringBuffer ("UPDATE I_PriceList "
			+ "SET X12DE355 = "
			+ "(SELECT MAX(X12DE355) FROM C_UOM u WHERE u.IsDefault='Y' AND u.AD_Client_ID IN (0,I_PriceList.AD_Client_ID)) "
			+ "WHERE X12DE355 IS NULL AND C_UOM_ID IS NULL"
			+ " AND I_IsImported<>'Y'").append(clientCheck);
		no = DB.executeUpdate(sql.toString(), get_TrxName());
		log.fine("Set UOM Default=" + no);
		//
		sql = new StringBuffer ("UPDATE I_PriceList "
			+ "SET C_UOM_ID = (SELECT C_UOM_ID FROM C_UOM u WHERE u.X12DE355=I_PriceList.X12DE355 AND u.AD_Client_ID IN (0,I_PriceList.AD_Client_ID)) "
			+ "WHERE C_UOM_ID IS NULL"
			+ " AND I_IsImported<>'Y'").append(clientCheck);
		no = DB.executeUpdate(sql.toString(), get_TrxName());
		log.info("Set UOM=" + no);
		//
		sql = new StringBuffer ("UPDATE I_PriceList "
			+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid UOM, ' "
			+ "WHERE C_UOM_ID IS NULL"
			+ " AND I_IsImported<>'Y'").append(clientCheck);
		no = DB.executeUpdate(sql.toString(), get_TrxName());
		if (no != 0)
			log.warning("Invalid UOM=" + no);
		*/
    //	Set Currency
    sql = new StringBuffer("UPDATE I_PriceList " + "SET ISO_Code=(SELECT ISO_Code FROM C_Currency c" + " INNER JOIN C_AcctSchema a ON (a.C_Currency_ID=c.C_Currency_ID)" + " INNER JOIN AD_ClientInfo ci ON (a.C_AcctSchema_ID=ci.C_AcctSchema1_ID)" + " WHERE ci.AD_Client_ID=I_PriceList.AD_Client_ID) " + "WHERE C_Currency_ID IS NULL AND ISO_Code IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.fine("Set Currency Default=" + no);
    //
    sql = new StringBuffer("UPDATE I_PriceList " + "SET C_Currency_ID=(SELECT C_Currency_ID FROM C_Currency c" + " WHERE I_PriceList.ISO_Code=c.ISO_Code AND c.AD_Client_ID IN (0,I_PriceList.AD_Client_ID)) " + "WHERE C_Currency_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    log.info("doIt- Set Currency=" + no);
    //
    sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Currency,' " + "WHERE C_Currency_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("Invalid Currency=" + no);
    //	Mandatory Name or PriceListID
    sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Mandatory Name or PriceListID,' " + "WHERE Name IS NULL AND M_PriceList_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No Mandatory Name=" + no);
    //	Mandatory ValidFrom or PriceListVersionID
    sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Mandatory ValidFrom or PriceListVersionID,' " + "WHERE ValidFrom IS NULL AND M_PriceList_Version_ID IS NULL" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No Mandatory ValidFrom=" + no);
    //	Mandatory BreakValue if BPartner set
    sql = new StringBuffer("UPDATE I_PriceList " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Mandatory BreakValue,' " + "WHERE BreakValue IS NULL AND (C_BPartner_ID IS NOT NULL OR BPartner_Value IS NOT NULL)" + " AND I_IsImported<>'Y'").append(clientCheck);
    no = DB.executeUpdate(sql.toString(), get_TrxName());
    if (no != 0)
        log.warning("No Mandatory BreakValue=" + no);
    commitEx();
    //	-------------------------------------------------------------------
    int noInsertpp = 0;
    int noUpdatepp = 0;
    int noInsertppvb = 0;
    int noUpdateppvb = 0;
    int noInsertpl = 0;
    int noInsertplv = 0;
    //	Go through Records
    log.fine("start inserting/updating ...");
    sql = new StringBuffer("SELECT * FROM I_PriceList WHERE I_IsImported='N'").append(clientCheck);
    PreparedStatement pstmt_setImported = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        //	Set Imported = Y
        pstmt_setImported = DB.prepareStatement("UPDATE I_PriceList SET I_IsImported='Y', M_PriceList_ID=?, M_PriceList_Version_ID=?, " + "Updated=SysDate, Processed='Y' WHERE I_PriceList_ID=?", get_TrxName());
        //
        pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
        rs = pstmt.executeQuery();
        while (rs.next()) {
            X_I_PriceList imp = new X_I_PriceList(getCtx(), rs, get_TrxName());
            int I_PriceList_ID = imp.getI_PriceList_ID();
            int M_PriceList_ID = imp.getM_PriceList_ID();
            if (M_PriceList_ID == 0) {
                // try to obtain the ID directly from DB
                M_PriceList_ID = DB.getSQLValue(get_TrxName(), "SELECT M_PriceList_ID FROM M_PriceList WHERE IsActive='Y' AND AD_Client_ID=? AND Name=?", m_AD_Client_ID, imp.getName());
                if (M_PriceList_ID < 0)
                    M_PriceList_ID = 0;
            }
            boolean newPriceList = M_PriceList_ID == 0;
            log.fine("I_PriceList_ID=" + I_PriceList_ID + ", M_PriceList_ID=" + M_PriceList_ID);
            MPriceList pricelist = null;
            //	PriceList
            if (//	Insert new Price List
            newPriceList) {
                pricelist = new MPriceList(imp);
                if (pricelist.save()) {
                    M_PriceList_ID = pricelist.getM_PriceList_ID();
                    log.finer("Insert Price List");
                    noInsertpl++;
                } else {
                    StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert Price List failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
                    DB.executeUpdate(sql0.toString(), get_TrxName());
                    continue;
                }
            } else {
                // NOTE no else clause - if the price list already exists it's not updated
                pricelist = new MPriceList(getCtx(), M_PriceList_ID, get_TrxName());
            }
            int M_PriceList_Version_ID = imp.getM_PriceList_Version_ID();
            if (M_PriceList_Version_ID == 0) {
                // try to obtain the ID directly from DB
                M_PriceList_Version_ID = DB.getSQLValue(get_TrxName(), "SELECT M_PriceList_Version_ID FROM M_PriceList_Version WHERE IsActive='Y' AND ValidFrom=? AND M_PriceList_ID=?", new Object[] { imp.getValidFrom(), M_PriceList_ID });
                if (M_PriceList_Version_ID < 0)
                    M_PriceList_Version_ID = 0;
            }
            boolean newPriceListVersion = M_PriceList_Version_ID == 0;
            log.fine("I_PriceList_ID=" + I_PriceList_ID + ", M_PriceList_Version_ID=" + M_PriceList_Version_ID);
            MPriceListVersion pricelistversion = null;
            //	PriceListVersion
            if (//	Insert new Price List Version
            newPriceListVersion) {
                pricelistversion = new MPriceListVersion(pricelist);
                pricelistversion.setValidFrom(imp.getValidFrom());
                pricelistversion.setName(pricelist.getName() + " " + imp.getValidFrom());
                pricelistversion.setM_DiscountSchema_ID(m_discountschema_id);
                if (pricelistversion.save()) {
                    M_PriceList_Version_ID = pricelistversion.getM_PriceList_Version_ID();
                    log.finer("Insert Price List Version");
                    noInsertplv++;
                } else {
                    StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert Price List Version failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
                    DB.executeUpdate(sql0.toString(), get_TrxName());
                    continue;
                }
            } else {
                // NOTE no else clause - if the price list version already exists it's not updated
                pricelistversion = new MPriceListVersion(getCtx(), M_PriceList_Version_ID, get_TrxName());
            }
            // If bpartner then insert/update into M_ProductPriceVendorBreak, otherwise insert/update M_ProductPrice
            if (imp.getC_BPartner_ID() > 0) {
                // M_ProductPriceVendorBreak
                int M_ProductPriceVendorBreak_ID = DB.getSQLValue(get_TrxName(), "SELECT M_ProductPriceVendorBreak_ID " + "FROM M_ProductPriceVendorBreak " + "WHERE M_PriceList_Version_ID=? AND " + "IsActive='Y' AND " + "C_BPartner_ID=? AND " + "M_Product_ID=? AND " + "BreakValue=?", new Object[] { pricelistversion.getM_PriceList_Version_ID(), imp.getC_BPartner_ID(), imp.getM_Product_ID(), imp.getBreakValue() });
                if (M_ProductPriceVendorBreak_ID < 0)
                    M_ProductPriceVendorBreak_ID = 0;
                X_M_ProductPriceVendorBreak ppvb = new X_M_ProductPriceVendorBreak(getCtx(), M_ProductPriceVendorBreak_ID, get_TrxName());
                boolean isInsert = false;
                if (M_ProductPriceVendorBreak_ID == 0) {
                    ppvb.setM_PriceList_Version_ID(pricelistversion.getM_PriceList_Version_ID());
                    ppvb.setC_BPartner_ID(imp.getC_BPartner_ID());
                    ppvb.setM_Product_ID(imp.getM_Product_ID());
                    ppvb.setBreakValue(imp.getBreakValue());
                    isInsert = true;
                }
                if (p_importPriceLimit)
                    ppvb.setPriceLimit(imp.getPriceLimit());
                if (p_importPriceList)
                    ppvb.setPriceList(imp.getPriceList());
                if (p_importPriceStd)
                    ppvb.setPriceStd(imp.getPriceStd());
                if (ppvb.save()) {
                    if (isInsert)
                        noInsertppvb++;
                    else
                        noUpdateppvb++;
                    log.finer("Insert/Update Product Price Vendor Break");
                } else {
                    StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert/Update Product Price Vendor Break Version failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
                    DB.executeUpdate(sql0.toString(), get_TrxName());
                    continue;
                }
            } else {
                // M_ProductPrice
                MProductPrice pp = MProductPrice.get(getCtx(), pricelistversion.getM_PriceList_Version_ID(), imp.getM_Product_ID(), get_TrxName());
                boolean isInsert = false;
                if (pp != null) {
                    if (p_importPriceLimit)
                        pp.setPriceLimit(imp.getPriceLimit());
                    if (p_importPriceList)
                        pp.setPriceList(imp.getPriceList());
                    if (p_importPriceStd)
                        pp.setPriceStd(imp.getPriceStd());
                } else {
                    pp = new MProductPrice(pricelistversion, imp.getM_Product_ID(), p_importPriceList ? imp.getPriceList() : Env.ZERO, p_importPriceStd ? imp.getPriceStd() : Env.ZERO, p_importPriceLimit ? imp.getPriceLimit() : Env.ZERO);
                    isInsert = true;
                }
                if (pp.save()) {
                    log.finer("Insert/Update Product Price");
                    if (isInsert)
                        noInsertpp++;
                    else
                        noUpdatepp++;
                } else {
                    StringBuffer sql0 = new StringBuffer("UPDATE I_PriceList i " + "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert/Update Product Price failed")).append("WHERE I_PriceList_ID=").append(I_PriceList_ID);
                    DB.executeUpdate(sql0.toString(), get_TrxName());
                    continue;
                }
            }
            //	Update I_PriceList
            pstmt_setImported.setInt(1, M_PriceList_ID);
            pstmt_setImported.setInt(2, M_PriceList_Version_ID);
            pstmt_setImported.setInt(3, I_PriceList_ID);
            no = pstmt_setImported.executeUpdate();
            //
            commitEx();
        }
    //	for all I_PriceList
    //
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
        DB.close(pstmt_setImported);
        pstmt_setImported = null;
    }
    //	Set Error to indicator to not imported
    sql = new StringBuffer("UPDATE I_PriceList " + "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(noInsertpl), "@M_PriceList_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noInsertplv), "@M_PriceList_Version_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noInsertpp), "Product Price: @Inserted@");
    addLog(0, null, new BigDecimal(noUpdatepp), "Product Price: @Updated@");
    addLog(0, null, new BigDecimal(noInsertppvb), "@M_ProductPriceVendorBreak_ID@: @Inserted@");
    addLog(0, null, new BigDecimal(noUpdateppvb), "@M_ProductPriceVendorBreak_ID@: @Updated@");
    return "";
}
Also used : X_I_PriceList(org.compiere.model.X_I_PriceList) AdempiereUserError(org.compiere.util.AdempiereUserError) PreparedStatement(java.sql.PreparedStatement) MPriceListVersion(org.compiere.model.MPriceListVersion) MPriceList(org.compiere.model.MPriceList) BigDecimal(java.math.BigDecimal) ResultSet(java.sql.ResultSet) X_M_ProductPriceVendorBreak(org.compiere.model.X_M_ProductPriceVendorBreak) MProductPrice(org.compiere.model.MProductPrice)

Example 2 with MPriceList

use of org.compiere.model.MPriceList 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 3 with MPriceList

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

the class InventoryUtil method getCreateProductPrice.

public static MProductPrice getCreateProductPrice(String ctxPriceList, int M_Product_ID, int price) {
    Properties ctx = Env.getCtx();
    int M_PriceList_ID = Env.getContextAsInt(ctx, ctxPriceList);
    MPriceList pl = MPriceList.get(ctx, M_PriceList_ID, null);
    MPriceListVersion plv = pl.getPriceListVersion(null);
    //
    BigDecimal priceBD = BigDecimal.valueOf(price);
    MProductPrice pp = MProductPrice.get(ctx, plv.get_ID(), M_Product_ID, null);
    if (pp == null) {
        pp = new MProductPrice(plv, M_Product_ID, priceBD, priceBD, priceBD);
    }
    pp.setPrices(priceBD, priceBD, priceBD);
    pp.saveEx();
    return pp;
}
Also used : MPriceListVersion(org.compiere.model.MPriceListVersion) MPriceList(org.compiere.model.MPriceList) Properties(java.util.Properties) BigDecimal(java.math.BigDecimal) MProductPrice(org.compiere.model.MProductPrice)

Example 4 with MPriceList

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

the class ProductNotOnPriceListException method buildMessage.

private static final String buildMessage(MProductPricing pp, int documentLineNo) {
    StringBuffer sb = new StringBuffer();
    if (documentLineNo > 0) {
        if (sb.length() > 0)
            sb.append(", ");
        sb.append("@Line@:").append(documentLineNo);
    }
    if (pp.getM_Product_ID() > 0) {
        MProduct p = MProduct.get(Env.getCtx(), pp.getM_Product_ID());
        if (sb.length() > 0)
            sb.append(", ");
        sb.append("@M_Product_ID@:").append(p == null ? "?" : p.get_Translation(MProduct.COLUMNNAME_Name));
    }
    if (pp.getM_PriceList_ID() > 0) {
        MPriceList pl = MPriceList.get(Env.getCtx(), pp.getM_PriceList_ID(), null);
        if (sb.length() > 0)
            sb.append(", ");
        sb.append("@M_PriceList_ID@:").append(pl == null ? "?" : pl.get_Translation(MPriceList.COLUMNNAME_Name));
    }
    if (pp.getPriceDate() != null) {
        DateFormat df = DisplayType.getDateFormat(DisplayType.Date);
        if (sb.length() > 0)
            sb.append(", ");
        sb.append("@Date@:").append(df.format(pp.getPriceDate()));
    }
    //
    sb.insert(0, "@" + AD_Message + "@ - ");
    return sb.toString();
}
Also used : MProduct(org.compiere.model.MProduct) DateFormat(java.text.DateFormat) MPriceList(org.compiere.model.MPriceList)

Example 5 with MPriceList

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

the class InventoryUtil method getCreateProductPrice.

public static MProductPrice getCreateProductPrice(String ctxPriceList, int M_Product_ID, int price) {
    Properties ctx = Env.getCtx();
    int M_PriceList_ID = Env.getContextAsInt(ctx, ctxPriceList);
    MPriceList pl = MPriceList.get(ctx, M_PriceList_ID, null);
    MPriceListVersion plv = pl.getPriceListVersion(null);
    //
    BigDecimal priceBD = BigDecimal.valueOf(price);
    MProductPrice pp = MProductPrice.get(ctx, plv.get_ID(), M_Product_ID, null);
    if (pp == null) {
        pp = new MProductPrice(plv, M_Product_ID, priceBD, priceBD, priceBD);
    }
    pp.setPrices(priceBD, priceBD, priceBD);
    pp.saveEx();
    return pp;
}
Also used : MPriceListVersion(org.compiere.model.MPriceListVersion) MPriceList(org.compiere.model.MPriceList) Properties(java.util.Properties) BigDecimal(java.math.BigDecimal) MProductPrice(org.compiere.model.MProductPrice)

Aggregations

MPriceList (org.compiere.model.MPriceList)10 MPriceListVersion (org.compiere.model.MPriceListVersion)9 BigDecimal (java.math.BigDecimal)5 MProductPrice (org.compiere.model.MProductPrice)5 Properties (java.util.Properties)4 Query (org.compiere.model.Query)3 Timestamp (java.sql.Timestamp)2 MProduct (org.compiere.model.MProduct)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 DateFormat (java.text.DateFormat)1 TreeMap (java.util.TreeMap)1 MAccount (org.compiere.model.MAccount)1 MDistributionRun (org.compiere.model.MDistributionRun)1 MDistributionRunLine (org.compiere.model.MDistributionRunLine)1 MElementValue (org.compiere.model.MElementValue)1 MWarehouse (org.compiere.model.MWarehouse)1 X_I_PriceList (org.compiere.model.X_I_PriceList)1 X_M_ProductPriceVendorBreak (org.compiere.model.X_M_ProductPriceVendorBreak)1 X_M_Product_Acct (org.compiere.model.X_M_Product_Acct)1