diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/exception/IExceptionMessage.java b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/exception/IExceptionMessage.java index 45bfde9..e0fc4dd 100644 --- a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/exception/IExceptionMessage.java +++ b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/exception/IExceptionMessage.java @@ -59,6 +59,13 @@ public interface IExceptionMessage { String PURCHASE_ORDERS_MISSING_PRINTING_SETTINGS = /*$$(*/ "Please fill printing settings on following purchase orders: %s" /*)*/; + String NO_PURCHASE_REQUEST_SELECTED_FOR_PRINTING = /*$$(*/ + "Please select the purchase request(s) to print." /*)*/; + String PURCHASE_REQUEST_MISSING_PRINTING_SETTINGS = /*$$(*/ + "Please fill printing settings on purchase request %s" /*)*/; + String PURCHASE_REQUESTS_MISSING_PRINTING_SETTINGS = /*$$(*/ + "Please fill printing settings on following purchase requests: %s" /*)*/; + public static final String PURCHASE_REQUEST_1 = /*$$(*/ "There is no sequence set for the purchase requests for the company %s" /*)*/; public static final String PURCHASE_REQUEST_MISSING_SUPPLIER_USER = /*$$(*/ diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/module/PurchaseModule.java b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/module/PurchaseModule.java index c4f1838..c311c2f 100644 --- a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/module/PurchaseModule.java +++ b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/module/PurchaseModule.java @@ -35,7 +35,9 @@ import com.axelor.apps.purchase.service.PurchaseRequestServiceImpl; import com.axelor.apps.purchase.service.app.AppPurchaseService; import com.axelor.apps.purchase.service.app.AppPurchaseServiceImpl; import com.axelor.apps.purchase.service.print.PurchaseOrderPrintService; +import com.axelor.apps.purchase.service.print.PurchaseRequestPrintService; import com.axelor.apps.purchase.service.print.PurchaseOrderPrintServiceImpl; +import com.axelor.apps.purchase.service.print.PurchaseRequestPrintServiceImpl; public class PurchaseModule extends AxelorModule { @@ -47,6 +49,7 @@ public class PurchaseModule extends AxelorModule { bind(PurchaseRequestService.class).to(PurchaseRequestServiceImpl.class); bind(PurchaseProductService.class).to(PurchaseProductServiceImpl.class); bind(PurchaseOrderPrintService.class).to(PurchaseOrderPrintServiceImpl.class); + bind(PurchaseRequestPrintService.class).to(PurchaseRequestPrintServiceImpl.class); bind(ProductServiceImpl.class).to(ProductServicePurchaseImpl.class); bind(PurchaseRequestRepository.class).to(PurchaseRequestManagementRepository.class); bind(PurchaseOrderLineService.class).to(PurchaseOrderLineServiceImpl.class); diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/report/IReport.java b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/report/IReport.java index bd238eb..d4565e2 100644 --- a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/report/IReport.java +++ b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/report/IReport.java @@ -20,4 +20,5 @@ package com.axelor.apps.purchase.report; public interface IReport { public static final String PURCHASE_ORDER = "PurchaseOrder.rptdesign"; + public static final String PURCHASE_REQUEST = "PurchaseRequest.rptdesign"; } diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/service/print/PurchaseRequestPrintService.java b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/service/print/PurchaseRequestPrintService.java new file mode 100644 index 0000000..81e1d67 --- /dev/null +++ b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/service/print/PurchaseRequestPrintService.java @@ -0,0 +1,39 @@ +/* + * Axelor Business Solutions + * + * Copyright (C) 2019 Axelor (). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.axelor.apps.purchase.service.print; + +import com.axelor.apps.purchase.db.PurchaseRequest; +import com.axelor.exception.AxelorException; +import java.io.IOException; +import java.util.List; + +public interface PurchaseRequestPrintService { + + /** + * Print a purchase order + * + * @return ReportSettings + * @throws IOException + * @throws AxelorException + */ + String printPurchaseRequests(List ids) throws IOException; + + String printPurchaseRequest(PurchaseRequest purchaseRequest, String formatPdf) throws AxelorException; + + String getFileName(PurchaseRequest purchaseRequest); +} diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/service/print/PurchaseRequestPrintServiceImpl.java b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/service/print/PurchaseRequestPrintServiceImpl.java new file mode 100644 index 0000000..921ce5d --- /dev/null +++ b/modules/axelor-open-suite/axelor-purchase/src/main/java/com/axelor/apps/purchase/service/print/PurchaseRequestPrintServiceImpl.java @@ -0,0 +1,110 @@ +/* + * Axelor Business Solutions + * + * Copyright (C) 2019 Axelor (). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.axelor.apps.purchase.service.print; + +import com.axelor.apps.ReportFactory; +import com.axelor.apps.base.service.app.AppBaseService; +import com.axelor.apps.purchase.db.PurchaseRequest; +import com.axelor.apps.purchase.exception.IExceptionMessage; +import com.axelor.apps.purchase.report.IReport; +import com.axelor.apps.report.engine.ReportSettings; +import com.axelor.apps.tool.ModelTool; +import com.axelor.apps.tool.ThrowConsumer; +import com.axelor.apps.tool.file.PdfTool; +import com.axelor.exception.AxelorException; +import com.axelor.exception.db.repo.TraceBackRepository; +import com.axelor.i18n.I18n; +import com.axelor.inject.Beans; +import java.io.File; +import java.io.IOException; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class PurchaseRequestPrintServiceImpl implements PurchaseRequestPrintService { + + @Override + public String printPurchaseRequest(PurchaseRequest purchaseRequest, String formatPdf) + throws AxelorException { + + String fileName = getPurchaseRequestFilesName(false, formatPdf); + + return PdfTool.getFileLinkFromPdfFile(print(purchaseRequest, formatPdf), fileName); + } + + @Override + public String printPurchaseRequests(List ids) throws IOException { + List printedPurchaseRequests = new ArrayList<>(); + ModelTool.apply( + PurchaseRequest.class, + ids, + new ThrowConsumer() { + + @Override + public void accept(PurchaseRequest purchaseRequest) throws Exception { + printedPurchaseRequests.add(print(purchaseRequest, ReportSettings.FORMAT_PDF)); + } + }); + String fileName = getPurchaseRequestFilesName(true, ReportSettings.FORMAT_PDF); + return PdfTool.mergePdfToFileLink(printedPurchaseRequests, fileName); + } + + public File print(PurchaseRequest purchaseRequest, String formatPdf) throws AxelorException { + ReportSettings reportSettings = prepareReportSettings(purchaseRequest, formatPdf); + return reportSettings.generate().getFile(); + } + + public ReportSettings prepareReportSettings(PurchaseRequest purchaseRequest, String formatPdf) + throws AxelorException { + if (purchaseRequest.getPrintingSettings() == null) { + throw new AxelorException( + TraceBackRepository.CATEGORY_MISSING_FIELD, + String.format( + I18n.get(IExceptionMessage.PURCHASE_REQUEST_MISSING_PRINTING_SETTINGS), + purchaseRequest.getPurchaseRequestSeq()), + purchaseRequest); + } + String locale = ReportSettings.getPrintingLocale(null); + String title = getFileName(purchaseRequest); + ReportSettings reportSetting = + ReportFactory.createReport(IReport.PURCHASE_REQUEST, title + " - ${date}"); + + + return reportSetting + .addParam("PurchaseRequestId", purchaseRequest.getId()) + .addParam("Locale", locale) + .addParam("HeaderHeight", purchaseRequest.getPrintingSettings().getPdfHeaderHeight()) + .addParam("FooterHeight", purchaseRequest.getPrintingSettings().getPdfFooterHeight()) + .addFormat(formatPdf); + } + + protected String getPurchaseRequestFilesName(boolean plural, String formatPdf) { + return I18n.get(plural ? "Purchase requests" : "Purchase request") + + " - " + + Beans.get(AppBaseService.class).getTodayDate().format(DateTimeFormatter.BASIC_ISO_DATE) + + "." + + formatPdf; + } + + @Override + public String getFileName(PurchaseRequest purchaseRequest) { + return I18n.get("Purchase request") + + " " + + purchaseRequest.getPurchaseRequestSeq(); + } +} 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 e318092..1858947 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 @@ -35,10 +35,27 @@ import com.axelor.rpc.ActionResponse; import com.google.inject.Singleton; import java.util.List; import java.util.stream.Collectors; +import com.axelor.apps.purchase.service.print.PurchaseRequestPrintService; +import com.axelor.common.ObjectUtils; +import com.axelor.rpc.Context; +import com.google.common.base.Function; +import java.util.List; +import javax.annotation.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.axelor.apps.report.engine.ReportSettings; +import com.axelor.exception.service.TraceBackService; +import com.google.common.collect.Lists; +import java.lang.invoke.MethodHandles; + + @Singleton public class PurchaseRequestController { + private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public void confirmCart(ActionRequest request, ActionResponse response) { Beans.get(PurchaseRequestService.class).confirmCart(); response.setReload(true); @@ -147,4 +164,51 @@ public class PurchaseRequestController { } + /** + * Called from grid or form purchase order view, print selected purchase order. + * + * @param request + * @param response + * @return + */ + @SuppressWarnings("unchecked") + public void showPurchaseRequest(ActionRequest request, ActionResponse response) { + + Context context = request.getContext(); + String fileLink; + String title; + PurchaseRequestPrintService purchaseRequestPrintService = + Beans.get(PurchaseRequestPrintService.class); + + try { + if (!ObjectUtils.isEmpty(request.getContext().get("_ids"))) { + List ids = + Lists.transform( + (List) request.getContext().get("_ids"), + new Function() { + @Nullable + @Override + public Long apply(@Nullable Object input) { + return Long.parseLong(input.toString()); + } + }); + fileLink = purchaseRequestPrintService.printPurchaseRequests(ids); + title = I18n.get("Purchase requests"); + } else if (context.get("id") != null) { + PurchaseRequest purchaseRequest = request.getContext().asType(PurchaseRequest.class); + title = purchaseRequestPrintService.getFileName(purchaseRequest); + fileLink = + purchaseRequestPrintService.printPurchaseRequest(purchaseRequest, ReportSettings.FORMAT_PDF); + logger.debug("Printing " + title); + } else { + throw new AxelorException( + TraceBackRepository.CATEGORY_MISSING_FIELD, + I18n.get(IExceptionMessage.NO_PURCHASE_REQUEST_SELECTED_FOR_PRINTING)); + } + response.setView(ActionView.define(title).add("html", fileLink).map()); + } catch (Exception e) { + TraceBackService.trace(response, e); + } + } + } diff --git a/modules/axelor-open-suite/axelor-purchase/src/main/resources/domains/PurchaseRequest.xml b/modules/axelor-open-suite/axelor-purchase/src/main/resources/domains/PurchaseRequest.xml index 3bf65a7..d6b2856 100644 --- a/modules/axelor-open-suite/axelor-purchase/src/main/resources/domains/PurchaseRequest.xml +++ b/modules/axelor-open-suite/axelor-purchase/src/main/resources/domains/PurchaseRequest.xml @@ -31,6 +31,8 @@ + +