Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRDP-260] Change mapping payer info #98

Merged
merged 8 commits into from
Dec 4, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.gov.pagopa.receipt.pdf.generator.entity.event;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class InfoTransaction {

private String brand;
private String brandLogo;
private String clientId;
private String paymentMethodName;
private String type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public class Transaction {
private String numAut;
private String accountCode;
private TransactionPsp psp;
private String origin;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public class TransactionDetails {
private User user;
private Transaction transaction;
private WalletItem wallet;
private InfoTransaction info;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
private String fullName;
private String name;
private String surname;
private UserType type;
private String fiscalCode;
private String notificationEmail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@
import it.gov.pagopa.receipt.pdf.generator.entity.receipt.enumeration.ReasonErrorCode;
import it.gov.pagopa.receipt.pdf.generator.exception.PdfJsonMappingException;
import it.gov.pagopa.receipt.pdf.generator.exception.TemplateDataMappingException;
import it.gov.pagopa.receipt.pdf.generator.model.template.Cart;
import it.gov.pagopa.receipt.pdf.generator.model.template.Debtor;
import it.gov.pagopa.receipt.pdf.generator.model.template.Item;
import it.gov.pagopa.receipt.pdf.generator.model.template.PSP;
import it.gov.pagopa.receipt.pdf.generator.model.template.PSPFee;
import it.gov.pagopa.receipt.pdf.generator.model.template.Payee;
import it.gov.pagopa.receipt.pdf.generator.model.template.PaymentMethod;
import it.gov.pagopa.receipt.pdf.generator.model.template.ReceiptPDFTemplate;
import it.gov.pagopa.receipt.pdf.generator.model.template.RefNumber;
import it.gov.pagopa.receipt.pdf.generator.model.template.Transaction;
import it.gov.pagopa.receipt.pdf.generator.model.template.User;
import it.gov.pagopa.receipt.pdf.generator.model.template.UserData;
import it.gov.pagopa.receipt.pdf.generator.model.template.*;
import it.gov.pagopa.receipt.pdf.generator.service.BuildTemplateService;
import it.gov.pagopa.receipt.pdf.generator.utils.ObjectMapperUtils;
import it.gov.pagopa.receipt.pdf.generator.utils.TemplateDataField;
Expand All @@ -31,12 +20,7 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.TimeZone;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -87,7 +71,9 @@ public class BuildTemplateServiceImpl implements BuildTemplateService {
* {@inheritDoc}
*/
@Override
public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean partialTemplate, Receipt receipt) throws TemplateDataMappingException {
public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean isGeneratingDebtor, Receipt receipt) throws TemplateDataMappingException {
boolean requestedByDebtor = getRequestByDebtor(isGeneratingDebtor, bizEvent);

return ReceiptPDFTemplate.builder()
.serviceCustomerId(getServiceCustomerId(bizEvent))
.transaction(Transaction.builder()
Expand All @@ -101,10 +87,10 @@ public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean partialTempla
.accountHolder(getPaymentMethodAccountHolder(bizEvent))
.build())
.authCode(getAuthCode(bizEvent))
.requestedByDebtor(partialTemplate)
.requestedByDebtor(requestedByDebtor)
.processedByPagoPA(getProcessedByPagoPA(bizEvent))
.build())
.user(partialTemplate ?
.user(requestedByDebtor ?
null :
User.builder()
.data(UserData.builder()
Expand Down Expand Up @@ -228,13 +214,30 @@ private String getPaymentMethodAccountHolder(BizEvent event) {
}

private String getUserFullName(BizEvent event) throws TemplateDataMappingException {
if (
event.getTransactionDetails() != null &&
event.getTransactionDetails().getUser() != null &&
event.getTransactionDetails().getUser().getName() != null &&
event.getTransactionDetails().getUser().getSurname() != null
) {
return String.format("%s %s",
event.getTransactionDetails().getUser().getName(),
event.getTransactionDetails().getUser().getSurname());
}
if (event.getPayer() != null && event.getPayer().getFullName() != null) {
return event.getPayer().getFullName();
}
throw new TemplateDataMappingException(formatErrorMessage(TemplateDataField.USER_DATA_FULL_NAME), ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode());
}

private String getUserTaxCode(BizEvent event) throws TemplateDataMappingException {
if (
event.getTransactionDetails() != null &&
event.getTransactionDetails().getUser() != null &&
event.getTransactionDetails().getUser().getFiscalCode() != null
) {
return event.getTransactionDetails().getUser().getFiscalCode();
}
if (event.getPayer() != null && event.getPayer().getEntityUniqueIdentifierValue() != null) {
return event.getPayer().getEntityUniqueIdentifierValue();
}
Expand Down Expand Up @@ -347,8 +350,41 @@ private String getOrThrow(LinkedHashMap<String, String> map, String key, String
return value;
}

public boolean getProcessedByPagoPA(BizEvent event) {
return event.getIdPaymentManager() != null && !event.getIdPaymentManager().isBlank();
private boolean getProcessedByPagoPA(BizEvent event) {
if (event.getTransactionDetails() != null) {
if (event.getTransactionDetails().getTransaction() != null &&
event.getTransactionDetails().getTransaction().getOrigin() != null) {
return true;
}
if (event.getTransactionDetails().getInfo() != null &&
event.getTransactionDetails().getInfo().getClientId() != null) {
return true;
}
}
return false;
}

private boolean getRequestByDebtor(boolean isGeneratingDebtor, BizEvent event) {
if (isGeneratingDebtor) {
String debtorFiscalCode = event.getDebtor().getEntityUniqueIdentifierValue();

String fiscalCodeFromPayer = event.getPayer() != null ? event.getPayer().getEntityUniqueIdentifierValue() : null;
String fiscalCodeFromUser = event.getTransactionDetails() != null && event.getTransactionDetails().getUser() != null ?
event.getTransactionDetails().getUser().getFiscalCode() : null;
//Check if payer's and user's fiscal codes exist
if (fiscalCodeFromPayer == null && fiscalCodeFromUser == null) {
return true;
}
//Check if payer's fiscal code exists and is different from debtor's
if (fiscalCodeFromPayer != null && !fiscalCodeFromPayer.equals(debtorFiscalCode)) {
return true;
}
//Check if user's fiscal code exists and is different from debtor's
if (fiscalCodeFromUser != null && !fiscalCodeFromUser.equals(debtorFiscalCode)) {
return true;
}
}
return false;
}

private String currencyFormat(String value) {
Expand All @@ -372,6 +408,7 @@ private String dateFormatZoned(String date) throws TemplateDataMappingException
SimpleDateFormat dateFormat = new SimpleDateFormat(RECEIPT_DATE_FORMAT, Locale.ITALY);
return dateFormat.format(parsed);
}

private String dateFormat(String date) {
DateTimeFormatter simpleDateFormat = DateTimeFormatter.ofPattern(RECEIPT_DATE_FORMAT).withLocale(Locale.ITALY);
return LocalDateTime.parse(date).format(simpleDateFormat);
Expand All @@ -392,6 +429,6 @@ private String formatFullName(String fullName, String fiscalCode) {
return null;
}

return fullName.replaceAll("[,;:/]+", " ");
return fullName.replaceAll("[,;:/]+", " ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public PdfGeneration generateReceipts(Receipt receipt, BizEvent bizEvent, Path w
pdfGeneration.setDebtorMetadata(PdfMetadata.builder().statusCode(ALREADY_CREATED).build());
return pdfGeneration;
}
PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt, PAYER_TEMPLATE_SUFFIX, false, workingDirPath);
PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt, PAYER_TEMPLATE_SUFFIX, true, workingDirPath);
pdfGeneration.setDebtorMetadata(generationResult);
return pdfGeneration;
}
Expand Down Expand Up @@ -166,9 +166,9 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati
return result;
}

private PdfMetadata generateAndSavePDFReceipt(BizEvent bizEvent, Receipt receipt, String templateSuffix, boolean partialTemplate, Path workingDirPath) {
private PdfMetadata generateAndSavePDFReceipt(BizEvent bizEvent, Receipt receipt, String templateSuffix, boolean isGeneratingDebtor, Path workingDirPath) {
try {
ReceiptPDFTemplate template = buildTemplateService.buildTemplate(bizEvent, partialTemplate, receipt);
ReceiptPDFTemplate template = buildTemplateService.buildTemplate(bizEvent, isGeneratingDebtor, receipt);
String dateFormatted = LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd"));
String blobName = String.format("%s-%s-%s-%s", TEMPLATE_PREFIX, dateFormatted, bizEvent.getId(), templateSuffix);
PdfEngineResponse pdfEngineResponse = generatePDFReceipt(template, workingDirPath);
Expand Down
Loading
Loading