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);
}
}
}
}
}
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));
}
}
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);
}
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;
}
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;
}
}
Aggregations