diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/web/PurchaseRequestController.java b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/web/PurchaseRequestController.java index c8e4ab3..dfac1e2 100644 --- a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/web/PurchaseRequestController.java +++ b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/web/PurchaseRequestController.java @@ -20,7 +20,9 @@ package com.axelor.apps.purchase.web; import com.axelor.apps.message.service.MailAccountService; import com.axelor.apps.purchase.db.PurchaseOrder; import com.axelor.apps.purchase.db.PurchaseRequest; +import com.axelor.apps.purchase.db.PurchaseRequestLine; import com.axelor.apps.purchase.db.repo.PurchaseRequestRepository; +import com.axelor.apps.purchase.db.repo.PurchaseRequestLineRepository; import com.axelor.apps.purchase.exception.IExceptionMessage; import com.axelor.apps.purchase.service.PurchaseRequestService; import com.axelor.apps.tool.StringTool; @@ -241,5 +243,39 @@ public class PurchaseRequestController { public void sendEmail(String email,String subject,String body){ EmailUtil.sendEmail(mailAccountService, email,subject, body); } + + public void checkProductFamily(ActionRequest request, ActionResponse response) throws AxelorException { + + Long id = request.getContext().asType(PurchaseRequest.class).getId(); + PurchaseRequest purchaseRequest = Beans.get(PurchaseRequestRepository.class).find(id); + String productFamily = purchaseRequest.getFamilleProduit().getName().toString(); + Integer size = 0; + + List purchaseRequestLines = + Beans.get(PurchaseRequestLineRepository.class) + .all() + .filter("self.newProduct = ?1 and self.purchaseRequest = ?2",false,id) + .fetch(); + + if (purchaseRequestLines != null){ + + size = purchaseRequestLines.stream() + .collect(Collectors.groupingBy(fa -> fa.getProduct().getFamilleProduit())).size(); + + } + if (size > 1){ + response.setError(I18n.get("Lines of expression of needs are not of the same family :") +productFamily); + //return; + } + if (size == 1){ + long idOfFamily = purchaseRequestLines.stream() + .collect(Collectors.groupingBy(fa -> fa.getProduct().getFamilleProduit().getId())) + .keySet().stream().collect(Collectors.toList()).get(0); + + if (purchaseRequest.getFamilleProduit().getId() != idOfFamily){ + response.setError(I18n.get("Lines of expression of needs are not of the same family :") +productFamily); + } + } + } }