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

[PIDM-42] fix(payment-status): payment position/option status not coherent #289

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[google-java-format] reported by reviewdog 🐶

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[google-java-format] reported by reviewdog 🐶

PaymentOption paymentOption = po.get();
// Update PaymentPosition instance only in memory
// PaymentPosition used when converting PaymentOption to POWithDebtor
DebtPositionStatus.validityCheckAndUpdate(paymentOption);
DebtPositionStatus.expirationCheckAndUpdate(paymentOption);
DebtPositionStatus.checkAlreadyPaidInstallments(paymentOption, nav);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[google-java-format] reported by reviewdog 🐶

pp.setStatus(DebtPositionStatus.PAID);
pp.setPaymentDate(paymentOptionModel.getPaymentDate());

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javax.validation.Valid;
Expand Down Expand Up @@ -233,7 +234,10 @@ private PaymentOption updatePaymentStatus(PaymentPosition pp, String nav, Paymen
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[google-java-format] reported by reviewdog 🐶

Suggested change
}
pp.setLastUpdatedDate(currentDate);
// salvo l'aggiornamento del pagamento
paymentPositionRepository.saveAndFlush(pp);
return poToPay;
}
private Transfer updateTransferStatus(PaymentPosition pp, String iuv, String transferId) {
LocalDateTime currentDate = LocalDateTime.now(ZoneOffset.UTC);
long numberPOTransfers = 0;
long countReportedTransfer = 0;
Transfer reportedTransfer = null;
for (PaymentOption po : pp.getPaymentOption()) {
if (po.getIuv().equals(iuv)) {
// numero totale dei transfer per la PO
numberPOTransfers = po.getTransfer().stream().count();
// numero dei transfer della PO in stato T_REPORTED
countReportedTransfer =
po.getTransfer().stream()
.filter(t -> t.getStatus().equals(TransferStatus.T_REPORTED))
.count();
// recupero il transfer oggetto di rendicontazione
Optional<Transfer> transferToReport =
po.getTransfer().stream().filter(t -> t.getIdTransfer().equals(transferId)).findFirst();
if (transferToReport.isEmpty()) {
log.error(
"Obtained unexpected empty transfer - "
+ "[organizationFiscalCode= "
+ pp.getOrganizationFiscalCode()
+ "; "
+ "iupd= "
+ pp.getIupd()
+ "; "
+ "iuv= "
+ iuv
+ "; "
+ "idTransfer= "
+ transferId
+ "]");
throw new AppException(
AppError.TRANSFER_REPORTING_FAILED, pp.getOrganizationFiscalCode(), iuv, transferId);
}


// aggiorno lo stato della payment position
if (countPaidPartialPayment > 0 && countPaidPartialPayment < numberOfPartialPayment) {
// PIDM-42 if paying the full amount when there is already a paid partial payment
// then update the payment position status to PAID
if (countPaidPartialPayment > 0 && countPaidPartialPayment < numberOfPartialPayment
&& Boolean.TRUE.equals(Objects.requireNonNull(poToPay).getIsPartialPayment())) {
pp.setStatus(DebtPositionStatus.PARTIALLY_PAID);
Comment on lines 237 to 242
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[google-java-format] reported by reviewdog 🐶

Suggested change
// aggiorno lo stato della payment position
if (countPaidPartialPayment > 0 && countPaidPartialPayment < numberOfPartialPayment) {
// PIDM-42 if paying the full amount when there is already a paid partial payment
// then update the payment position status to PAID
if (countPaidPartialPayment > 0 && countPaidPartialPayment < numberOfPartialPayment
&& Boolean.TRUE.equals(Objects.requireNonNull(poToPay).getIsPartialPayment())) {
pp.setStatus(DebtPositionStatus.PARTIALLY_PAID);
transferToReport.get().setStatus(TransferStatus.T_REPORTED);
transferToReport.get().setLastUpdatedDate(currentDate);
countReportedTransfer++;
// aggiorno lo stato della PO
if (countReportedTransfer < numberPOTransfers) {
po.setStatus(PaymentOptionStatus.PO_PARTIALLY_REPORTED);

} else {
pp.setStatus(DebtPositionStatus.PAID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,28 @@
}

// La posizione debitoria è già in PARTIALLY_PAID ed arriva una richiesta di pagamento su una payment option non rateizzata (isPartialPayment = false) => errore
if (ppToPay.getStatus().equals(DebtPositionStatus.PARTIALLY_PAID) && Boolean.FALSE.equals(poToPay.getIsPartialPayment())) {
throw new AppException(AppError.PAYMENT_OPTION_ALREADY_PAID, poToPay.getOrganizationFiscalCode(), nav);
// PIDM-42: if this is a full payment and the position is partially paid then
// log this but allow the payment option status to be changed to PO_PAID instead of throwing an error
if (ppToPay.getStatus().equals(DebtPositionStatus.PARTIALLY_PAID)
&& Boolean.FALSE.equals(poToPay.getIsPartialPayment())) {

// log detailed information about this edge case
log.warn("Potential payment state inconsistency detected || " +
"Organization: {} || " +
"IUPD: {} || " +
"NAV: {} || " +
"Position Status: {} || " +
"Payment Option Status: {} || " +
"Is Partial Payment: {} || " +
"Timestamp: {}",
ppToPay.getOrganizationFiscalCode(),
ppToPay.getIupd(),
nav,
ppToPay.getStatus(),
poToPay.getStatus(),
poToPay.getIsPartialPayment(),
LocalDateTime.now());
Fixed Show fixed Hide fixed
}

}

private static boolean isPaid(PaymentOption po) {
Expand Down