Search in sources :

Example 1 with ConfiguratorBOM

use of com.axelor.apps.production.db.ConfiguratorBOM in project axelor-open-suite by axelor.

the class ConfiguratorServiceProductionImpl method generate.

/**
 * In this implementation, we also create a bill of materials.
 *
 * @param configurator
 * @param jsonAttributes
 * @param jsonIndicators
 * @throws AxelorException
 */
@Override
@Transactional(rollbackOn = { Exception.class })
public void generate(Configurator configurator, JsonContext jsonAttributes, JsonContext jsonIndicators) throws AxelorException {
    super.generate(configurator, jsonAttributes, jsonIndicators);
    ConfiguratorBOM configuratorBOM = configurator.getConfiguratorCreator().getConfiguratorBom();
    if (configuratorBOM != null) {
        Product generatedProduct = configurator.getProduct();
        Beans.get(ConfiguratorBomService.class).generateBillOfMaterial(configuratorBOM, jsonAttributes, 0, generatedProduct).ifPresent(generatedProduct::setDefaultBillOfMaterial);
    }
}
Also used : ConfiguratorBOM(com.axelor.apps.production.db.ConfiguratorBOM) Product(com.axelor.apps.base.db.Product) Transactional(com.google.inject.persist.Transactional)

Example 2 with ConfiguratorBOM

use of com.axelor.apps.production.db.ConfiguratorBOM in project axelor-open-suite by axelor.

the class ConfiguratorServiceProductionImpl method generateSaleOrderLine.

/**
 * In this implementation, we also create a bill of material.
 */
@Override
protected SaleOrderLine generateSaleOrderLine(Configurator configurator, JsonContext jsonAttributes, JsonContext jsonIndicators, SaleOrder saleOrder) throws AxelorException {
    SaleOrderLine saleOrderLine = super.generateSaleOrderLine(configurator, jsonAttributes, jsonIndicators, saleOrder);
    ConfiguratorBOM configuratorBOM = configurator.getConfiguratorCreator().getConfiguratorBom();
    if (configuratorBOM != null) {
        Beans.get(ConfiguratorBomService.class).generateBillOfMaterial(configuratorBOM, jsonAttributes, 0, null).ifPresent(saleOrderLine::setBillOfMaterial);
    }
    return saleOrderLine;
}
Also used : ConfiguratorBOM(com.axelor.apps.production.db.ConfiguratorBOM) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine)

Example 3 with ConfiguratorBOM

use of com.axelor.apps.production.db.ConfiguratorBOM in project axelor-open-suite by axelor.

the class ConfiguratorBomServiceImpl method generateBillOfMaterial.

@Override
@Transactional(rollbackOn = { Exception.class })
public Optional<BillOfMaterial> generateBillOfMaterial(ConfiguratorBOM configuratorBOM, JsonContext attributes, int level, Product generatedProduct) throws AxelorException {
    level++;
    if (level > MAX_LEVEL) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONFIGURATOR_BOM_TOO_MANY_CALLS));
    }
    String name;
    Product product;
    BigDecimal qty;
    Unit unit;
    ProdProcess prodProcess;
    if (!checkConditions(configuratorBOM, attributes)) {
        return Optional.empty();
    }
    if (configuratorBOM.getDefNameAsFormula()) {
        name = (String) configuratorService.computeFormula(configuratorBOM.getNameFormula(), attributes);
    } else {
        name = configuratorBOM.getName();
    }
    if (configuratorBOM.getDefProductFromConfigurator()) {
        if (generatedProduct == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONFIGURATOR_BOM_IMPORT_GENERATED_PRODUCT_NULL));
        }
        product = generatedProduct;
    } else if (configuratorBOM.getDefProductAsFormula()) {
        product = (Product) configuratorService.computeFormula(configuratorBOM.getProductFormula(), attributes);
        if (product == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONFIGURATOR_BOM_IMPORT_FORMULA_PRODUCT_NULL));
        }
        product = Beans.get(ProductRepository.class).find(product.getId());
    } else {
        if (configuratorBOM.getProduct() == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CONFIGURATOR_BOM_IMPORT_FILLED_PRODUCT_NULL));
        }
        product = configuratorBOM.getProduct();
    }
    if (configuratorBOM.getDefQtyAsFormula()) {
        qty = new BigDecimal(configuratorService.computeFormula(configuratorBOM.getQtyFormula(), attributes).toString());
    } else {
        qty = configuratorBOM.getQty();
    }
    if (configuratorBOM.getDefUnitAsFormula()) {
        unit = (Unit) configuratorService.computeFormula(configuratorBOM.getUnitFormula(), attributes);
        if (unit != null) {
            unit = Beans.get(UnitRepository.class).find(unit.getId());
        }
    } else {
        unit = configuratorBOM.getUnit();
    }
    if (configuratorBOM.getDefProdProcessAsFormula()) {
        prodProcess = (ProdProcess) configuratorService.computeFormula(configuratorBOM.getProdProcessFormula(), attributes);
        if (prodProcess != null) {
            prodProcess = Beans.get(ProdProcessRepository.class).find(prodProcess.getId());
        }
    } else if (configuratorBOM.getDefProdProcessAsConfigurator()) {
        prodProcess = confProdProcessService.generateProdProcessService(configuratorBOM.getConfiguratorProdProcess(), attributes, product);
    } else {
        prodProcess = configuratorBOM.getProdProcess();
    }
    BillOfMaterial billOfMaterial = new BillOfMaterial();
    billOfMaterial.setCompany(configuratorBOM.getCompany());
    billOfMaterial.setName(name);
    billOfMaterial.setProduct(product);
    billOfMaterial.setQty(qty);
    billOfMaterial.setUnit(unit);
    billOfMaterial.setProdProcess(prodProcess);
    billOfMaterial.setStatusSelect(configuratorBOM.getStatusSelect());
    billOfMaterial.setDefineSubBillOfMaterial(configuratorBOM.getDefineSubBillOfMaterial());
    if (configuratorBOM.getConfiguratorBomList() != null) {
        for (ConfiguratorBOM confBomChild : configuratorBOM.getConfiguratorBomList()) {
            generateBillOfMaterial(confBomChild, attributes, level, generatedProduct).ifPresent(billOfMaterial::addBillOfMaterialSetItem);
        }
    }
    billOfMaterial = billOfMaterialRepository.save(billOfMaterial);
    configuratorBOM.setBillOfMaterialId(billOfMaterial.getId());
    configuratorBOMRepo.save(configuratorBOM);
    return Optional.of(billOfMaterial);
}
Also used : AxelorException(com.axelor.exception.AxelorException) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Product(com.axelor.apps.base.db.Product) ConfiguratorBOM(com.axelor.apps.production.db.ConfiguratorBOM) ProdProcess(com.axelor.apps.production.db.ProdProcess) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 4 with ConfiguratorBOM

use of com.axelor.apps.production.db.ConfiguratorBOM in project axelor-open-suite by axelor.

the class ConfiguratorCreatorImportServiceProductionImpl method updateAttributeNameInFormulas.

/**
 * Update the changed attribute in all formula O2M. This implementation also update formulas in
 * configurator BOM and configurator prod process.
 *
 * @param creator
 * @param oldName
 * @param newName
 * @throws AxelorException
 */
@Override
protected void updateAttributeNameInFormulas(ConfiguratorCreator creator, String oldName, String newName) throws AxelorException {
    super.updateAttributeNameInFormulas(creator, oldName, newName);
    if (!Beans.get(AppProductionService.class).isApp("production")) {
        return;
    }
    ConfiguratorBOM configuratorBom = creator.getConfiguratorBom();
    if (configuratorBom != null) {
        updateAttributeNameInFormulas(configuratorBom, oldName, newName, 0);
    }
}
Also used : ConfiguratorBOM(com.axelor.apps.production.db.ConfiguratorBOM)

Example 5 with ConfiguratorBOM

use of com.axelor.apps.production.db.ConfiguratorBOM in project axelor-open-suite by axelor.

the class ConfiguratorCreatorImportServiceProductionImpl method updateAttributeNameInFormulas.

/**
 * Update attribute name in formulas for a configurator bom.
 *
 * @param configuratorBom
 * @param oldName
 * @param newName
 * @param counter used to count the recursive call.
 * @throws AxelorException if we got too many recursive call.
 */
protected void updateAttributeNameInFormulas(ConfiguratorBOM configuratorBom, String oldName, String newName, int counter) throws AxelorException {
    if (counter > MAX_DEPTH) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.CONFIGURATOR_BOM_IMPORT_TOO_MANY_CALLS));
    }
    updateAllFormulaFields(configuratorBom, oldName, newName);
    ConfiguratorProdProcess configuratorProdProcess = configuratorBom.getConfiguratorProdProcess();
    if (configuratorProdProcess != null) {
        updateAttributeNameInFormulas(configuratorBom.getConfiguratorProdProcess(), oldName, newName);
    }
    // recursive call for child BOMs
    List<ConfiguratorBOM> childConfiguratorBomList = Beans.get(ConfiguratorBOMRepository.class).all().filter("self.parentConfiguratorBOM.id = :parentId").bind("parentId", configuratorBom.getId()).fetch();
    if (childConfiguratorBomList != null) {
        for (ConfiguratorBOM childConfiguratorBom : childConfiguratorBomList) {
            updateAttributeNameInFormulas(childConfiguratorBom, oldName, newName, counter + 1);
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) ConfiguratorBOM(com.axelor.apps.production.db.ConfiguratorBOM) ConfiguratorProdProcess(com.axelor.apps.production.db.ConfiguratorProdProcess)

Aggregations

ConfiguratorBOM (com.axelor.apps.production.db.ConfiguratorBOM)5 Product (com.axelor.apps.base.db.Product)2 AxelorException (com.axelor.exception.AxelorException)2 Transactional (com.google.inject.persist.Transactional)2 Unit (com.axelor.apps.base.db.Unit)1 ProductRepository (com.axelor.apps.base.db.repo.ProductRepository)1 BillOfMaterial (com.axelor.apps.production.db.BillOfMaterial)1 ConfiguratorProdProcess (com.axelor.apps.production.db.ConfiguratorProdProcess)1 ProdProcess (com.axelor.apps.production.db.ProdProcess)1 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)1 BigDecimal (java.math.BigDecimal)1