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