Search in sources :

Example 36 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class ManufOrderServiceImpl method merge.

@Transactional(rollbackOn = { Exception.class })
public void merge(List<Long> ids) throws AxelorException {
    if (!canMerge(ids)) {
        throw new AxelorException(ManufOrder.class, TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.MANUF_ORDER_NO_GENERATION));
    }
    List<ManufOrder> manufOrderList = manufOrderRepo.all().filter("self.id in (" + Joiner.on(",").join(ids) + ")").fetch();
    /* Init all the necessary values to create the new Manuf Order */
    Product product = manufOrderList.get(0).getProduct();
    StockLocation stockLocation = manufOrderList.get(0).getWorkshopStockLocation();
    Company company = manufOrderList.get(0).getCompany();
    BillOfMaterial billOfMaterial = manufOrderList.stream().filter(x -> x.getBillOfMaterial().getVersionNumber() == 1).findFirst().get().getBillOfMaterial();
    int priority = manufOrderList.stream().mapToInt(ManufOrder::getPrioritySelect).max().orElse(2);
    Unit unit = billOfMaterial.getUnit();
    BigDecimal qty = BigDecimal.ZERO;
    String note = "";
    ManufOrder mergedManufOrder = new ManufOrder();
    for (ManufOrder manufOrder : manufOrderList) {
        manufOrder.setStatusSelect(ManufOrderRepository.STATUS_MERGED);
        manufOrder.setManufOrderMergeResult(mergedManufOrder);
        for (ProductionOrder productionOrder : manufOrder.getProductionOrderSet()) {
            mergedManufOrder.addProductionOrderSetItem(productionOrder);
        }
        for (SaleOrder saleOrder : manufOrder.getSaleOrderSet()) {
            mergedManufOrder.addSaleOrderSetItem(saleOrder);
        }
        /*
       * If unit are the same, then add the qty If not, convert the unit and get the converted qty
       */
        if (manufOrder.getUnit().equals(unit)) {
            qty = qty.add(manufOrder.getQty());
        } else {
            BigDecimal qtyConverted = Beans.get(UnitConversionService.class).convert(manufOrder.getUnit(), unit, manufOrder.getQty(), appBaseService.getNbDecimalDigitForQty(), null);
            qty = qty.add(qtyConverted);
        }
        if (manufOrder.getNote() != null && !manufOrder.getNote().equals("")) {
            note += manufOrder.getManufOrderSeq() + " : " + manufOrder.getNote() + "\n";
        }
    }
    Optional<LocalDateTime> minDate = manufOrderList.stream().filter(mo -> mo.getPlannedStartDateT() != null).map(ManufOrder::getPlannedStartDateT).min(LocalDateTime::compareTo);
    minDate.ifPresent(mergedManufOrder::setPlannedStartDateT);
    /* Update the created manuf order */
    mergedManufOrder.setStatusSelect(ManufOrderRepository.STATUS_DRAFT);
    mergedManufOrder.setProduct(product);
    mergedManufOrder.setUnit(unit);
    mergedManufOrder.setWorkshopStockLocation(stockLocation);
    mergedManufOrder.setQty(qty);
    mergedManufOrder.setBillOfMaterial(billOfMaterial);
    mergedManufOrder.setCompany(company);
    mergedManufOrder.setPrioritySelect(priority);
    mergedManufOrder.setProdProcess(billOfMaterial.getProdProcess());
    mergedManufOrder.setNote(note);
    /*
     * Check the config to see if you directly plan the created manuf order or just prefill the
     * operations
     */
    if (appProductionService.isApp("production") && appProductionService.getAppProduction().getIsManufOrderPlannedAfterMerge()) {
        manufOrderWorkflowService.plan(mergedManufOrder);
    } else {
        preFillOperations(mergedManufOrder);
    }
    manufOrderRepo.save(mergedManufOrder);
}
Also used : StockLocationRepository(com.axelor.apps.stock.db.repo.StockLocationRepository) IExceptionMessage(com.axelor.apps.production.exceptions.IExceptionMessage) ProductionConfig(com.axelor.apps.production.db.ProductionConfig) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) ProductCompanyService(com.axelor.apps.base.service.ProductCompanyService) Transactional(com.google.inject.persist.Transactional) BigDecimal(java.math.BigDecimal) Pair(org.apache.commons.lang3.tuple.Pair) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) ProductVariantService(com.axelor.apps.base.service.ProductVariantService) SaleOrder(com.axelor.apps.sale.db.SaleOrder) StockMoveLineService(com.axelor.apps.stock.service.StockMoveLineService) ProductionConfigService(com.axelor.apps.production.service.config.ProductionConfigService) StockLocationService(com.axelor.apps.stock.service.StockLocationService) RoundingMode(java.math.RoundingMode) ProdProcessLine(com.axelor.apps.production.db.ProdProcessLine) StockConfig(com.axelor.apps.stock.db.StockConfig) StockMove(com.axelor.apps.stock.db.StockMove) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Set(java.util.Set) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) StringUtils(com.axelor.common.StringUtils) Collectors(java.util.stream.Collectors) SequenceService(com.axelor.apps.base.service.administration.SequenceService) List(java.util.List) Product(com.axelor.apps.base.db.Product) ManufOrder(com.axelor.apps.production.db.ManufOrder) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) Optional(java.util.Optional) ManufOrderRepository(com.axelor.apps.production.db.repo.ManufOrderRepository) Joiner(com.google.common.base.Joiner) Company(com.axelor.apps.base.db.Company) AppProductionService(com.axelor.apps.production.service.app.AppProductionService) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) ProdProductRepository(com.axelor.apps.production.db.repo.ProdProductRepository) LocalDateTime(java.time.LocalDateTime) StockMoveService(com.axelor.apps.stock.service.StockMoveService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AxelorException(com.axelor.exception.AxelorException) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockLocation(com.axelor.apps.stock.db.StockLocation) I18n(com.axelor.i18n.I18n) ProdProcess(com.axelor.apps.production.db.ProdProcess) ProdProduct(com.axelor.apps.production.db.ProdProduct) ProductionOrder(com.axelor.apps.production.db.ProductionOrder) StringTool(com.axelor.apps.tool.StringTool) Sequence(com.axelor.apps.base.db.Sequence) Logger(org.slf4j.Logger) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) MoreObjects(com.google.common.base.MoreObjects) OperationOrder(com.axelor.apps.production.db.OperationOrder) StockConfigProductionService(com.axelor.apps.production.service.config.StockConfigProductionService) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Beans(com.axelor.inject.Beans) Unit(com.axelor.apps.base.db.Unit) OperationOrderService(com.axelor.apps.production.service.operationorder.OperationOrderService) Comparator(java.util.Comparator) OperationOrderStockMoveService(com.axelor.apps.production.service.operationorder.OperationOrderStockMoveService) Collections(java.util.Collections) LocalDateTime(java.time.LocalDateTime) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockLocation(com.axelor.apps.stock.db.StockLocation) Product(com.axelor.apps.base.db.Product) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) ProdProduct(com.axelor.apps.production.db.ProdProduct) Unit(com.axelor.apps.base.db.Unit) SaleOrder(com.axelor.apps.sale.db.SaleOrder) BigDecimal(java.math.BigDecimal) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) ManufOrder(com.axelor.apps.production.db.ManufOrder) ProductionOrder(com.axelor.apps.production.db.ProductionOrder) Transactional(com.google.inject.persist.Transactional)

Example 37 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class ProductionOrderSaleOrderServiceImpl method generateManufOrders.

@Override
public ProductionOrder generateManufOrders(ProductionOrder productionOrder, SaleOrderLine saleOrderLine) throws AxelorException {
    Product product = saleOrderLine.getProduct();
    if (saleOrderLine.getSaleSupplySelect() == ProductRepository.SALE_SUPPLY_PRODUCE && product != null && product.getProductTypeSelect().equals(ProductRepository.PRODUCT_TYPE_STORABLE)) {
        BillOfMaterial billOfMaterial = saleOrderLine.getBillOfMaterial();
        if (billOfMaterial == null) {
            billOfMaterial = product.getDefaultBillOfMaterial();
        }
        if (billOfMaterial == null && product.getParentProduct() != null) {
            billOfMaterial = product.getParentProduct().getDefaultBillOfMaterial();
        }
        if (billOfMaterial == null) {
            throw new AxelorException(saleOrderLine, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.PRODUCTION_ORDER_SALES_ORDER_NO_BOM), product.getName(), product.getCode());
        }
        if (billOfMaterial.getProdProcess() == null) {
            return null;
        }
        Unit unit = saleOrderLine.getProduct().getUnit();
        BigDecimal qty = saleOrderLine.getQty();
        if (unit != null && !unit.equals(saleOrderLine.getUnit())) {
            qty = unitConversionService.convert(saleOrderLine.getUnit(), unit, qty, qty.scale(), saleOrderLine.getProduct());
        }
        return generateManufOrders(productionOrder, billOfMaterial, qty, LocalDateTime.now(), saleOrderLine.getSaleOrder());
    }
    return null;
}
Also used : BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) AxelorException(com.axelor.exception.AxelorException) Product(com.axelor.apps.base.db.Product) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 38 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class ManufOrderWorkflowService method createPurchaseOrderLineProduction.

private void createPurchaseOrderLineProduction(OperationOrder operationOrder, PurchaseOrder purchaseOrder) throws AxelorException {
    UnitConversionService unitConversionService = Beans.get(UnitConversionService.class);
    PurchaseOrderLineService purchaseOrderLineService = Beans.get(PurchaseOrderLineService.class);
    PurchaseOrderLine purchaseOrderLine;
    BigDecimal quantity = BigDecimal.ONE;
    Unit startUnit = Beans.get(UnitRepository.class).all().filter("self.name = 'Hour' AND self.unitTypeSelect = 3").fetchOne();
    for (ProdHumanResource humanResource : operationOrder.getProdHumanResourceList()) {
        Product product = humanResource.getProduct();
        Unit purchaseUnit = product.getPurchasesUnit();
        if (purchaseUnit != null) {
            quantity = unitConversionService.convert(startUnit, purchaseUnit, new BigDecimal(humanResource.getDuration() / 3600), 0, humanResource.getProduct());
        }
        purchaseOrderLine = purchaseOrderLineService.createPurchaseOrderLine(purchaseOrder, product, null, null, quantity, purchaseUnit);
        purchaseOrder.getPurchaseOrderLineList().add(purchaseOrderLine);
    }
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) ProdHumanResource(com.axelor.apps.production.db.ProdHumanResource) UnitRepository(com.axelor.apps.base.db.repo.UnitRepository) Product(com.axelor.apps.base.db.Product) PurchaseOrderLineService(com.axelor.apps.purchase.service.PurchaseOrderLineService) ChronoUnit(java.time.temporal.ChronoUnit) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 39 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class CostSheetServiceImpl method getTotalQtyPerProductAndUnit.

protected Map<List<Object>, BigDecimal> getTotalQtyPerProductAndUnit(List<StockMoveLine> stockMoveLineList, LocalDate calculationDate, LocalDate previousCostSheetDate, int calculationType) {
    Map<List<Object>, BigDecimal> stockMoveLinePerProductAndUnitMap = new HashMap<>();
    if (stockMoveLineList == null) {
        return stockMoveLinePerProductAndUnitMap;
    }
    for (StockMoveLine stockMoveLine : stockMoveLineList) {
        StockMove stockMove = stockMoveLine.getStockMove();
        if (stockMove == null || StockMoveRepository.STATUS_REALIZED != stockMoveLine.getStockMove().getStatusSelect()) {
            continue;
        }
        if ((calculationType == CostSheetRepository.CALCULATION_PARTIAL_END_OF_PRODUCTION || calculationType == CostSheetRepository.CALCULATION_END_OF_PRODUCTION) && previousCostSheetDate != null && !previousCostSheetDate.isBefore(stockMove.getRealDate())) {
            continue;
        } else if (calculationType == CostSheetRepository.CALCULATION_WORK_IN_PROGRESS && calculationDate.isBefore(stockMove.getRealDate())) {
            continue;
        }
        Product productKey = stockMoveLine.getProduct();
        Unit unitKey = stockMoveLine.getUnit();
        List<Object> keys = new ArrayList<Object>();
        keys.add(productKey);
        keys.add(unitKey);
        BigDecimal qty = stockMoveLinePerProductAndUnitMap.get(keys);
        if (qty == null) {
            qty = BigDecimal.ZERO;
        }
        stockMoveLinePerProductAndUnitMap.put(keys, qty.add(stockMoveLine.getRealQty()));
    }
    return stockMoveLinePerProductAndUnitMap;
}
Also used : StockMove(com.axelor.apps.stock.db.StockMove) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) ProdProduct(com.axelor.apps.production.db.ProdProduct) Product(com.axelor.apps.base.db.Product) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) ArrayList(java.util.ArrayList) List(java.util.List) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 40 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class CostSheetServiceImpl method computeConsumedProduct.

protected void computeConsumedProduct(int bomLevel, LocalDate previousCostSheetDate, CostSheetLine parentCostSheetLine, List<StockMoveLine> consumedStockMoveLineList, List<ProdProduct> toConsumeProdProductList, BigDecimal ratio) throws AxelorException {
    CostSheet parentCostSheet = parentCostSheetLine.getCostSheet();
    int calculationTypeSelect = parentCostSheet.getCalculationTypeSelect();
    LocalDate calculationDate = parentCostSheet.getCalculationDate();
    Map<List<Object>, BigDecimal> consumedStockMoveLinePerProductAndUnit = getTotalQtyPerProductAndUnit(consumedStockMoveLineList, calculationDate, previousCostSheetDate, calculationTypeSelect);
    for (List<Object> keys : consumedStockMoveLinePerProductAndUnit.keySet()) {
        Iterator<Object> iterator = keys.iterator();
        Product product = (Product) iterator.next();
        Unit unit = (Unit) iterator.next();
        BigDecimal realQty = consumedStockMoveLinePerProductAndUnit.get(keys);
        if (product == null) {
            continue;
        }
        BigDecimal valuationQty = BigDecimal.ZERO;
        if (calculationTypeSelect == CostSheetRepository.CALCULATION_WORK_IN_PROGRESS) {
            BigDecimal plannedConsumeQty = computeTotalQtyPerUnit(toConsumeProdProductList, product, unit);
            valuationQty = realQty.subtract(plannedConsumeQty.multiply(ratio));
        }
        valuationQty = valuationQty.setScale(appBaseService.getNbDecimalDigitForQty(), RoundingMode.HALF_UP);
        if (valuationQty.compareTo(BigDecimal.ZERO) == 0) {
            continue;
        }
        costSheetLineService.createConsumedProductCostSheetLine(parentCostSheet.getManufOrder().getCompany(), product, unit, bomLevel, parentCostSheetLine, valuationQty, CostSheetService.ORIGIN_MANUF_ORDER, null);
    }
}
Also used : ProdProduct(com.axelor.apps.production.db.ProdProduct) Product(com.axelor.apps.base.db.Product) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) ArrayList(java.util.ArrayList) List(java.util.List) Unit(com.axelor.apps.base.db.Unit) LocalDate(java.time.LocalDate) CostSheet(com.axelor.apps.production.db.CostSheet) BigDecimal(java.math.BigDecimal)

Aggregations

Unit (com.axelor.apps.base.db.Unit)45 BigDecimal (java.math.BigDecimal)38 Product (com.axelor.apps.base.db.Product)19 AxelorException (com.axelor.exception.AxelorException)13 Transactional (com.google.inject.persist.Transactional)12 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)11 UnitConversionService (com.axelor.apps.base.service.UnitConversionService)9 Company (com.axelor.apps.base.db.Company)8 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)8 StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)8 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)6 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)6 StockLocation (com.axelor.apps.stock.db.StockLocation)6 StockMove (com.axelor.apps.stock.db.StockMove)6 LocalDate (java.time.LocalDate)6 List (java.util.List)6 ProdProduct (com.axelor.apps.production.db.ProdProduct)5 ProdResidualProduct (com.axelor.apps.production.db.ProdResidualProduct)5 ArrayList (java.util.ArrayList)5 BillOfMaterial (com.axelor.apps.production.db.BillOfMaterial)4