Search in sources :

Example 11 with BillOfMaterial

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

the class MrpServiceProductionImpl method assignProductLevel.

/**
 * Update the level of Bill of materials. The highest for each product (0: product with parent, 1:
 * product with a parent, 2: product with a parent that have a parent, ...)
 *
 * @param billOfMaterial
 * @param level
 */
protected void assignProductLevel(BillOfMaterial billOfMaterial, int level) throws AxelorException {
    if (level > 100) {
        if (billOfMaterial == null || billOfMaterial.getProduct() == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MRP_BOM_LEVEL_TOO_HIGH));
        } else {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MRP_BOM_LEVEL_TOO_HIGH_PRODUCT), billOfMaterial.getProduct().getFullName());
        }
    }
    Product product = billOfMaterial.getProduct();
    log.debug("Add product: {} for the level : {} ", product.getFullName(), level);
    this.productMap.put(product.getId(), this.getMaxLevel(product, level));
    level = level + 1;
    if (billOfMaterial.getBillOfMaterialSet() != null && !billOfMaterial.getBillOfMaterialSet().isEmpty()) {
        for (BillOfMaterial subBillOfMaterial : billOfMaterial.getBillOfMaterialSet()) {
            Product subProduct = subBillOfMaterial.getProduct();
            if (this.isMrpProduct(subProduct)) {
                this.assignProductLevel(subBillOfMaterial, level);
                if (subProduct.getDefaultBillOfMaterial() != null) {
                    this.assignProductLevel(subProduct.getDefaultBillOfMaterial(), level);
                }
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) Product(com.axelor.apps.base.db.Product) ProdProduct(com.axelor.apps.production.db.ProdProduct)

Example 12 with BillOfMaterial

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

the class ProdProcessService method validateProdProcess.

public void validateProdProcess(ProdProcess prodProcess, BillOfMaterial bom) throws AxelorException {
    Map<Product, BigDecimal> bomMap = new HashMap<Product, BigDecimal>();
    Set<BillOfMaterial> setBoM = MoreObjects.firstNonNull(bom.getBillOfMaterialSet(), Collections.emptySet());
    for (BillOfMaterial bomIt : setBoM) {
        bomMap.put(bomIt.getProduct(), bomIt.getQty());
    }
    List<ProdProcessLine> listPPL = MoreObjects.firstNonNull(prodProcess.getProdProcessLineList(), Collections.emptyList());
    for (ProdProcessLine prodProcessLine : listPPL) {
        List<ProdProduct> listPP = MoreObjects.firstNonNull(prodProcessLine.getToConsumeProdProductList(), Collections.emptyList());
        for (ProdProduct prodProduct : listPP) {
            if (!bomMap.containsKey(prodProduct.getProduct())) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PROD_PROCESS_USELESS_PRODUCT), prodProduct.getProduct().getName());
            }
            bomMap.put(prodProduct.getProduct(), bomMap.get(prodProduct.getProduct()).subtract(prodProduct.getQty()));
        }
    }
    Set<Product> keyList = bomMap.keySet();
    Map<Product, BigDecimal> copyMap = new HashMap<Product, BigDecimal>();
    List<String> nameProductList = new ArrayList<String>();
    for (Product product : keyList) {
        if (bomMap.get(product).compareTo(BigDecimal.ZERO) > 0) {
            copyMap.put(product, bomMap.get(product));
            nameProductList.add(product.getName());
        }
    }
    if (!copyMap.isEmpty()) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PROD_PROCESS_MISS_PRODUCT), Joiner.on(",").join(nameProductList));
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProdProduct(com.axelor.apps.production.db.ProdProduct) Product(com.axelor.apps.base.db.Product) ProdProduct(com.axelor.apps.production.db.ProdProduct) ProdProcessLine(com.axelor.apps.production.db.ProdProcessLine) BigDecimal(java.math.BigDecimal) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial)

Example 13 with BillOfMaterial

use of com.axelor.apps.production.db.BillOfMaterial 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 14 with BillOfMaterial

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

the class BillOfMaterialServiceImpl method customizeBillOfMaterial.

@Override
@Transactional(rollbackOn = { Exception.class })
public BillOfMaterial customizeBillOfMaterial(BillOfMaterial billOfMaterial, int depth) throws AxelorException {
    if (depth > 1000) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.MAX_DEPTH_REACHED));
    }
    if (billOfMaterial != null) {
        BillOfMaterial personalizedBOM = JPA.copy(billOfMaterial, true);
        billOfMaterialRepo.save(personalizedBOM);
        personalizedBOM.setName(personalizedBOM.getName() + " (" + I18n.get(IExceptionMessage.BOM_1) + " " + personalizedBOM.getId() + ")");
        personalizedBOM.setPersonalized(true);
        Set<BillOfMaterial> personalizedBOMSet = new HashSet<BillOfMaterial>();
        for (BillOfMaterial childBillOfMaterial : billOfMaterial.getBillOfMaterialSet()) {
            personalizedBOMSet.add(customizeBillOfMaterial(childBillOfMaterial, depth + 1));
        }
        personalizedBOM.setBillOfMaterialSet(personalizedBOMSet);
        return personalizedBOM;
    }
    return null;
}
Also used : AxelorException(com.axelor.exception.AxelorException) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 15 with BillOfMaterial

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

the class BillOfMaterialServiceImpl method addRawMaterials.

@Override
@Transactional
public void addRawMaterials(long billOfMaterialId, ArrayList<LinkedHashMap<String, Object>> rawMaterials) throws AxelorException {
    if (rawMaterials != null && !rawMaterials.isEmpty()) {
        BillOfMaterial billOfMaterial = billOfMaterialRepo.find(billOfMaterialId);
        int priority = 0;
        if (billOfMaterial.getBillOfMaterialSet() != null && !billOfMaterial.getBillOfMaterialSet().isEmpty()) {
            priority = Collections.max(billOfMaterial.getBillOfMaterialSet().stream().map(it -> it.getPriority()).collect(Collectors.toSet()));
        }
        for (LinkedHashMap<String, Object> rawMaterial : rawMaterials) {
            priority += 10;
            BillOfMaterial newComponent = createBomFromRawMaterial(Long.valueOf((int) rawMaterial.get("id")), priority);
            billOfMaterial.getBillOfMaterialSet().add(newComponent);
        }
    } else {
        return;
    }
}
Also used : IExceptionMessage(com.axelor.apps.production.exceptions.IExceptionMessage) AppProductionService(com.axelor.apps.production.service.app.AppProductionService) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) ProductCompanyService(com.axelor.apps.base.service.ProductCompanyService) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) ProductService(com.axelor.apps.base.service.ProductService) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) TempBomTree(com.axelor.apps.production.db.TempBomTree) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) CollectionUtils(org.apache.commons.collections.CollectionUtils) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) I18n(com.axelor.i18n.I18n) RoundingMode(java.math.RoundingMode) JPA(com.axelor.db.JPA) Logger(org.slf4j.Logger) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) MethodHandles(java.lang.invoke.MethodHandles) Set(java.util.Set) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) TempBomTreeRepository(com.axelor.apps.production.db.repo.TempBomTreeRepository) Collectors(java.util.stream.Collectors) List(java.util.List) IReport(com.axelor.apps.production.report.IReport) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) Beans(com.axelor.inject.Beans) Product(com.axelor.apps.base.db.Product) ReportFactory(com.axelor.apps.ReportFactory) Collections(java.util.Collections) BillOfMaterialRepository(com.axelor.apps.production.db.repo.BillOfMaterialRepository) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) Transactional(com.google.inject.persist.Transactional)

Aggregations

BillOfMaterial (com.axelor.apps.production.db.BillOfMaterial)36 Product (com.axelor.apps.base.db.Product)19 BigDecimal (java.math.BigDecimal)16 AxelorException (com.axelor.exception.AxelorException)15 Transactional (com.google.inject.persist.Transactional)11 ProdProduct (com.axelor.apps.production.db.ProdProduct)10 ArrayList (java.util.ArrayList)8 ProdResidualProduct (com.axelor.apps.production.db.ProdResidualProduct)7 BillOfMaterialService (com.axelor.apps.production.service.BillOfMaterialService)7 Unit (com.axelor.apps.base.db.Unit)4 ProductRepository (com.axelor.apps.base.db.repo.ProductRepository)4 ProdProcess (com.axelor.apps.production.db.ProdProcess)4 ProdProcessLine (com.axelor.apps.production.db.ProdProcessLine)4 ProductionOrder (com.axelor.apps.production.db.ProductionOrder)4 AppProductionService (com.axelor.apps.production.service.app.AppProductionService)4 ProductCompanyService (com.axelor.apps.base.service.ProductCompanyService)3 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)3 ManufOrder (com.axelor.apps.production.db.ManufOrder)3 UnitCostCalcLine (com.axelor.apps.production.db.UnitCostCalcLine)3 BillOfMaterialRepository (com.axelor.apps.production.db.repo.BillOfMaterialRepository)3