Search in sources :

Example 11 with MrpLine

use of com.axelor.apps.supplychain.db.MrpLine in project axelor-open-suite by axelor.

the class MrpServiceImpl method checkInsufficientCumulativeQty.

protected void checkInsufficientCumulativeQty(Product product, int counter) throws AxelorException {
    final int MAX_ITERATION = 1000;
    if (counter > MAX_ITERATION) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.MRP_TOO_MANY_ITERATIONS));
    }
    boolean doASecondPass = false;
    this.computeCumulativeQty(productRepository.find(product.getId()));
    JPA.clear();
    List<MrpLine> mrpLineList = mrpLineRepository.all().filter("self.mrp.id = ?1 AND self.product.id = ?2", mrp.getId(), product.getId()).order("maturityDate").order("mrpLineType.typeSelect").order("mrpLineType.sequence").order("id").fetch();
    for (MrpLine mrpLine : mrpLineList) {
        doASecondPass = this.checkInsufficientCumulativeQty(mrpLineRepository.find(mrpLine.getId()), productRepository.find(product.getId()), counter == 0);
        JPA.clear();
        if (doASecondPass) {
            break;
        }
    }
    if (doASecondPass) {
        this.checkInsufficientCumulativeQty(product, counter + 1);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) MrpLine(com.axelor.apps.supplychain.db.MrpLine)

Example 12 with MrpLine

use of com.axelor.apps.supplychain.db.MrpLine in project axelor-open-suite by axelor.

the class MrpServiceImpl method createPurchaseMrpLines.

@Transactional(rollbackOn = { Exception.class })
protected void createPurchaseMrpLines(Mrp mrp, PurchaseOrderLine purchaseOrderLine, MrpLineType purchaseOrderMrpLineType) throws AxelorException {
    PurchaseOrder purchaseOrder = purchaseOrderLine.getPurchaseOrder();
    LocalDate maturityDate = purchaseOrderLine.getEstimatedDelivDate();
    if (maturityDate == null) {
        maturityDate = purchaseOrder.getDeliveryDate();
    }
    if (maturityDate == null) {
        maturityDate = purchaseOrderLine.getDesiredDelivDate();
    }
    maturityDate = this.computeMaturityDate(maturityDate, purchaseOrderMrpLineType);
    if (this.isBeforeEndDate(maturityDate) || purchaseOrderMrpLineType.getIgnoreEndDate()) {
        Unit unit = purchaseOrderLine.getProduct().getUnit();
        BigDecimal qty = purchaseOrderLine.getQty().subtract(purchaseOrderLine.getReceivedQty());
        if (!unit.equals(purchaseOrderLine.getUnit())) {
            qty = Beans.get(UnitConversionService.class).convert(purchaseOrderLine.getUnit(), unit, qty, qty.scale(), purchaseOrderLine.getProduct());
        }
        MrpLine mrpLine = this.createMrpLine(mrp, purchaseOrderLine.getProduct(), purchaseOrderMrpLineType, qty, maturityDate, BigDecimal.ZERO, purchaseOrder.getStockLocation(), purchaseOrderLine);
        if (mrpLine != null) {
            mrpLine.setSupplierPartner(purchaseOrder.getSupplierPartner());
            mrpLineRepository.save(mrpLine);
        }
    }
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) MrpLine(com.axelor.apps.supplychain.db.MrpLine) Unit(com.axelor.apps.base.db.Unit) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 13 with MrpLine

use of com.axelor.apps.supplychain.db.MrpLine in project axelor-open-suite by axelor.

the class MrpServiceImpl method consolidateMrp.

protected void consolidateMrp() {
    List<MrpLine> mrpLineList = mrpLineRepository.all().filter("self.mrp.id = ?1", mrp.getId()).order("product.code").order("maturityDate").order("mrpLineType.typeSelect").order("mrpLineType.sequence").order("id").fetch();
    Map<List<Object>, MrpLine> map = Maps.newHashMap();
    MrpLine consolidateMrpLine = null;
    List<Object> keys = new ArrayList<>();
    for (MrpLine mrpLine : mrpLineList) {
        MrpLineType mrpLineType = mrpLine.getMrpLineType();
        keys.clear();
        keys.add(mrpLineType);
        keys.add(mrpLine.getProduct());
        keys.add(mrpLine.getMaturityDate());
        keys.add(mrpLine.getStockLocation());
        if (map.containsKey(keys)) {
            consolidateMrpLine = map.get(keys);
            consolidateMrpLine.setQty(consolidateMrpLine.getQty().add(mrpLine.getQty()));
            consolidateMrpLine.setCumulativeQty(consolidateMrpLine.getCumulativeQty().add(mrpLine.getCumulativeQty()));
            mrpLineRepository.remove(mrpLine);
        } else {
            map.put(keys, mrpLine);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MrpLine(com.axelor.apps.supplychain.db.MrpLine) List(java.util.List) ArrayList(java.util.ArrayList) MrpLineType(com.axelor.apps.supplychain.db.MrpLineType)

Example 14 with MrpLine

use of com.axelor.apps.supplychain.db.MrpLine in project axelor-open-suite by axelor.

the class MrpServiceImpl method createSaleOrderMrpLines.

@Transactional(rollbackOn = { Exception.class })
protected void createSaleOrderMrpLines(Mrp mrp, SaleOrderLine saleOrderLine, MrpLineType saleOrderMrpLineType, List<Integer> statusList) throws AxelorException {
    SaleOrder saleOrder = saleOrderLine.getSaleOrder();
    if (!this.stockLocationList.contains(saleOrder.getStockLocation())) {
        return;
    }
    if (!statusList.contains(saleOrder.getStatusSelect())) {
        return;
    }
    if (saleOrderLine.getDeliveryState() == SaleOrderLineRepository.DELIVERY_STATE_DELIVERED) {
        return;
    }
    LocalDate maturityDate = saleOrderLine.getEstimatedDelivDate();
    if (maturityDate == null) {
        maturityDate = saleOrder.getDeliveryDate();
    }
    if (maturityDate == null) {
        maturityDate = saleOrderLine.getDesiredDelivDate();
    }
    maturityDate = this.computeMaturityDate(maturityDate, saleOrderMrpLineType);
    if (this.isBeforeEndDate(maturityDate)) {
        Unit unit = saleOrderLine.getProduct().getUnit();
        BigDecimal qty = saleOrderLine.getQty().subtract(saleOrderLine.getDeliveredQty());
        if (!unit.equals(saleOrderLine.getUnit())) {
            qty = Beans.get(UnitConversionService.class).convert(saleOrderLine.getUnit(), unit, qty, saleOrderLine.getQty().scale(), saleOrderLine.getProduct());
        }
        MrpLine mrpLine = this.createMrpLine(mrp, saleOrderLine.getProduct(), saleOrderMrpLineType, qty, maturityDate, BigDecimal.ZERO, saleOrder.getStockLocation(), saleOrderLine);
        if (mrpLine != null) {
            mrpLineRepository.save(mrpLine);
        }
    }
}
Also used : MrpLine(com.axelor.apps.supplychain.db.MrpLine) SaleOrder(com.axelor.apps.sale.db.SaleOrder) Unit(com.axelor.apps.base.db.Unit) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 15 with MrpLine

use of com.axelor.apps.supplychain.db.MrpLine in project axelor-open-suite by axelor.

the class ProjectedStockServiceImpl method createProjectedStock.

@Transactional(rollbackOn = { Exception.class })
@Override
public List<MrpLine> createProjectedStock(Long productId, Long companyId, Long stockLocationId) throws AxelorException {
    Product product = Beans.get(ProductRepository.class).find(productId);
    Company company = Beans.get(CompanyRepository.class).find(companyId);
    StockLocation stockLocation = stockLocationRepository.find(stockLocationId);
    Mrp mrp = new Mrp();
    mrp.setStockLocation(findStockLocation(company, stockLocation));
    // If a company has no stockLocation
    if (mrp.getStockLocation() == null) {
        return Collections.emptyList();
    }
    mrp.addProductSetItem(product);
    mrp.setMrpTypeSelect(MrpRepository.MRP_TYPE_MRP);
    mrp = Beans.get(MrpRepository.class).save(mrp);
    mrp = Beans.get(MrpService.class).createProjectedStock(mrp, product, company, stockLocation);
    List<MrpLine> mrpLineList = Beans.get(MrpLineRepository.class).all().filter("self.mrp = ?1 AND self.product = ?2 AND self.qty != 0", mrp, product).order("maturityDate").order("mrpLineType.typeSelect").order("mrpLineType.sequence").order("id").fetch();
    if (mrpLineList.isEmpty()) {
        List<MrpLine> mrpLineListToDelete = Beans.get(MrpLineRepository.class).all().filter("self.mrp = ?1", mrp).fetch();
        removeMrpAndMrpLine(mrpLineListToDelete);
        return Collections.emptyList();
    }
    for (MrpLine mrpLine : mrpLineList) {
        mrpLine.setCompany(mrpLine.getStockLocation().getCompany());
        mrpLine.setUnit(mrpLine.getProduct().getUnit());
    }
    return mrpLineList;
}
Also used : MrpLineRepository(com.axelor.apps.supplychain.db.repo.MrpLineRepository) Company(com.axelor.apps.base.db.Company) CompanyRepository(com.axelor.apps.base.db.repo.CompanyRepository) StockLocation(com.axelor.apps.stock.db.StockLocation) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) Product(com.axelor.apps.base.db.Product) MrpLine(com.axelor.apps.supplychain.db.MrpLine) Mrp(com.axelor.apps.supplychain.db.Mrp) Transactional(com.google.inject.persist.Transactional)

Aggregations

MrpLine (com.axelor.apps.supplychain.db.MrpLine)19 Transactional (com.google.inject.persist.Transactional)10 LocalDate (java.time.LocalDate)6 BigDecimal (java.math.BigDecimal)4 Product (com.axelor.apps.base.db.Product)3 Unit (com.axelor.apps.base.db.Unit)3 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)3 ArrayList (java.util.ArrayList)3 ProductRepository (com.axelor.apps.base.db.repo.ProductRepository)2 StockLocation (com.axelor.apps.stock.db.StockLocation)2 MrpLineType (com.axelor.apps.supplychain.db.MrpLineType)2 MrpLineService (com.axelor.apps.supplychain.service.MrpLineService)2 ProjectedStockService (com.axelor.apps.supplychain.service.ProjectedStockService)2 AxelorException (com.axelor.exception.AxelorException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Company (com.axelor.apps.base.db.Company)1 Partner (com.axelor.apps.base.db.Partner)1 CompanyRepository (com.axelor.apps.base.db.repo.CompanyRepository)1 OperationOrder (com.axelor.apps.production.db.OperationOrder)1