add printing to purchase request

This commit is contained in:
bachir souldi
2022-07-26 12:14:09 +01:00
parent c5ba18e1c4
commit cabcdc49a1
7 changed files with 226 additions and 0 deletions

View File

@@ -59,6 +59,13 @@ public interface IExceptionMessage {
String PURCHASE_ORDERS_MISSING_PRINTING_SETTINGS = /*$$(*/ String PURCHASE_ORDERS_MISSING_PRINTING_SETTINGS = /*$$(*/
"Please fill printing settings on following purchase orders: %s" /*)*/; "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 = /*$$(*/ public static final String PURCHASE_REQUEST_1 = /*$$(*/
"There is no sequence set for the purchase requests for the company %s" /*)*/; "There is no sequence set for the purchase requests for the company %s" /*)*/;
public static final String PURCHASE_REQUEST_MISSING_SUPPLIER_USER = /*$$(*/ public static final String PURCHASE_REQUEST_MISSING_SUPPLIER_USER = /*$$(*/

View File

@@ -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.AppPurchaseService;
import com.axelor.apps.purchase.service.app.AppPurchaseServiceImpl; import com.axelor.apps.purchase.service.app.AppPurchaseServiceImpl;
import com.axelor.apps.purchase.service.print.PurchaseOrderPrintService; 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.PurchaseOrderPrintServiceImpl;
import com.axelor.apps.purchase.service.print.PurchaseRequestPrintServiceImpl;
public class PurchaseModule extends AxelorModule { public class PurchaseModule extends AxelorModule {
@@ -47,6 +49,7 @@ public class PurchaseModule extends AxelorModule {
bind(PurchaseRequestService.class).to(PurchaseRequestServiceImpl.class); bind(PurchaseRequestService.class).to(PurchaseRequestServiceImpl.class);
bind(PurchaseProductService.class).to(PurchaseProductServiceImpl.class); bind(PurchaseProductService.class).to(PurchaseProductServiceImpl.class);
bind(PurchaseOrderPrintService.class).to(PurchaseOrderPrintServiceImpl.class); bind(PurchaseOrderPrintService.class).to(PurchaseOrderPrintServiceImpl.class);
bind(PurchaseRequestPrintService.class).to(PurchaseRequestPrintServiceImpl.class);
bind(ProductServiceImpl.class).to(ProductServicePurchaseImpl.class); bind(ProductServiceImpl.class).to(ProductServicePurchaseImpl.class);
bind(PurchaseRequestRepository.class).to(PurchaseRequestManagementRepository.class); bind(PurchaseRequestRepository.class).to(PurchaseRequestManagementRepository.class);
bind(PurchaseOrderLineService.class).to(PurchaseOrderLineServiceImpl.class); bind(PurchaseOrderLineService.class).to(PurchaseOrderLineServiceImpl.class);

View File

@@ -20,4 +20,5 @@ package com.axelor.apps.purchase.report;
public interface IReport { public interface IReport {
public static final String PURCHASE_ORDER = "PurchaseOrder.rptdesign"; public static final String PURCHASE_ORDER = "PurchaseOrder.rptdesign";
public static final String PURCHASE_REQUEST = "PurchaseRequest.rptdesign";
} }

View File

@@ -0,0 +1,39 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2019 Axelor (<http://axelor.com>).
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Long> ids) throws IOException;
String printPurchaseRequest(PurchaseRequest purchaseRequest, String formatPdf) throws AxelorException;
String getFileName(PurchaseRequest purchaseRequest);
}

View File

@@ -0,0 +1,110 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2019 Axelor (<http://axelor.com>).
*
* 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 <http://www.gnu.org/licenses/>.
*/
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<Long> ids) throws IOException {
List<File> printedPurchaseRequests = new ArrayList<>();
ModelTool.apply(
PurchaseRequest.class,
ids,
new ThrowConsumer<PurchaseRequest>() {
@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();
}
}

View File

@@ -35,10 +35,27 @@ import com.axelor.rpc.ActionResponse;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; 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 @Singleton
public class PurchaseRequestController { public class PurchaseRequestController {
private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public void confirmCart(ActionRequest request, ActionResponse response) { public void confirmCart(ActionRequest request, ActionResponse response) {
Beans.get(PurchaseRequestService.class).confirmCart(); Beans.get(PurchaseRequestService.class).confirmCart();
response.setReload(true); 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<Long> ids =
Lists.transform(
(List) request.getContext().get("_ids"),
new Function<Object, Long>() {
@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);
}
}
} }

View File

@@ -31,6 +31,8 @@
<many-to-one name="assignedToUser" title="Buyer" ref="com.axelor.auth.db.User"/> <many-to-one name="assignedToUser" title="Buyer" ref="com.axelor.auth.db.User"/>
<many-to-one name="printingSettings" ref="com.axelor.apps.base.db.PrintingSettings"/>
<unique-constraint columns="purchaseRequestSeq"/> <unique-constraint columns="purchaseRequestSeq"/>
<extra-code> <extra-code>