Search in sources :

Example 1 with PurchaseRequestLine

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;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PurchaseRequestLine(com.axelor.apps.purchase.db.PurchaseRequestLine) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) Product(com.axelor.apps.base.db.Product) PurchaseRequest(com.axelor.apps.purchase.db.PurchaseRequest) Transactional(com.google.inject.persist.Transactional)

Aggregations

Product (com.axelor.apps.base.db.Product)1 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)1 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)1 PurchaseRequest (com.axelor.apps.purchase.db.PurchaseRequest)1 PurchaseRequestLine (com.axelor.apps.purchase.db.PurchaseRequestLine)1 Transactional (com.google.inject.persist.Transactional)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1