use of com.axelor.apps.purchase.db.PurchaseRequestLine in project axelor-open-suite by axelor.
the class PurchaseRequestServiceImpl method generatePo.
@Transactional(rollbackOn = { Exception.class })
@Override
public List<PurchaseOrder> generatePo(List<PurchaseRequest> purchaseRequests, Boolean groupBySupplier, Boolean groupByProduct) throws AxelorException {
List<PurchaseOrderLine> purchaseOrderLineList = new ArrayList<PurchaseOrderLine>();
Map<String, PurchaseOrder> purchaseOrderMap = new HashMap<>();
for (PurchaseRequest purchaseRequest : purchaseRequests) {
PurchaseOrder purchaseOrder;
String key = groupBySupplier ? getPurchaseOrderGroupBySupplierKey(purchaseRequest) : null;
if (key != null && purchaseOrderMap.containsKey(key)) {
purchaseOrder = purchaseOrderMap.get(key);
} else {
purchaseOrder = createPurchaseOrder(purchaseRequest);
key = key == null ? purchaseRequest.getId().toString() : key;
purchaseOrderMap.put(key, purchaseOrder);
}
if (purchaseOrder == null) {
purchaseOrder = createPurchaseOrder(purchaseRequest);
}
for (PurchaseRequestLine purchaseRequestLine : purchaseRequest.getPurchaseRequestLineList()) {
PurchaseOrderLine purchaseOrderLine = new PurchaseOrderLine();
Product product = purchaseRequestLine.getProduct();
purchaseOrderLine = groupByProduct && purchaseOrder != null ? getPoLineByProduct(product, purchaseOrder) : null;
purchaseOrderLine = purchaseOrderLineService.createPurchaseOrderLine(purchaseOrder, product, purchaseRequestLine.getNewProduct() ? purchaseRequestLine.getProductTitle() : null, null, purchaseRequestLine.getQuantity(), purchaseRequestLine.getUnit());
purchaseOrder.addPurchaseOrderLineListItem(purchaseOrderLine);
purchaseOrderLineList.add(purchaseOrderLine);
purchaseOrderLineService.compute(purchaseOrderLine, purchaseOrder);
}
purchaseOrder.getPurchaseOrderLineList().addAll(purchaseOrderLineList);
purchaseOrderService.computePurchaseOrder(purchaseOrder);
purchaseOrderRepo.save(purchaseOrder);
purchaseRequest.setPurchaseOrder(purchaseOrder);
purchaseRequestRepo.save(purchaseRequest);
}
List<PurchaseOrder> purchaseOrders = purchaseOrderMap.values().stream().collect(Collectors.toList());
return purchaseOrders;
}
Aggregations