Skip to content

Commit

Permalink
Merge pull request #1670 from hmcts/PAY-7103-NEW
Browse files Browse the repository at this point in the history
PAY-7103: Calculate the AMOUNT DUE and OVERPAYMENTS for ( Issue refun…
  • Loading branch information
Thor-tech-of-metal committed Aug 22, 2024
2 parents eddb307 + aedd72f commit f7dc134
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/src/contractTest/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ logging:
org.springframework.web: INFO
au.com.dius.pact: DEBUG
pattern:
console: "%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n"
console: "%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n"
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
Expand All @@ -20,6 +22,7 @@
import uk.gov.hmcts.payment.api.dto.PaymentGroupDto;
import uk.gov.hmcts.payment.api.dto.PaymentGroupResponse;
import uk.gov.hmcts.payment.api.dto.PaymentSearchCriteria;
import uk.gov.hmcts.payment.api.dto.RefundListDtoResponse;
import uk.gov.hmcts.payment.api.dto.mapper.PaymentDtoMapper;
import uk.gov.hmcts.payment.api.dto.mapper.PaymentGroupDtoMapper;
import uk.gov.hmcts.payment.api.model.PaymentFeeLink;
Expand All @@ -41,6 +44,8 @@
@Validated
public class CaseController {

private static final Logger LOG = LoggerFactory.getLogger(CaseController.class);

private final PaymentService<PaymentFeeLink, String> paymentService;
private final PaymentGroupService<PaymentFeeLink, String> paymentGroupService;
private final PaymentDtoMapper paymentDtoMapper;
Expand Down Expand Up @@ -113,6 +118,11 @@ public PaymentGroupResponse retrieveCasePaymentGroups(@PathVariable(name = "ccdc

paymentGroupResponse = paymentRefundsService.checkRefundAgainstRemissionV2(headers, paymentGroupResponse, ccdCaseNumber);


RefundListDtoResponse refundListDtoResponse = paymentRefundsService.getRefundsApprovedFromRefundService(ccdCaseNumber,headers);
if (refundListDtoResponse != null) {
paymentGroups.stream().forEach(paymentGroup -> paymentGroup.setRefunds(refundListDtoResponse.getRefundList()));
}
return paymentGroupResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ public class PaymentGroupDto {

private boolean isAnyPaymentDisputed;

private List<RefundDto> refunds;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ ResponseEntity<RefundResponse> createAndValidateRetrospectiveRemissionRequest(


void deleteByRefundReference(String refundReference, MultiValueMap<String, String> headers);

RefundListDtoResponse getRefundsApprovedFromRefundService(String ccdCaseNumber, MultiValueMap<String, String> headers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.time.temporal.ChronoUnit;

import org.apache.commons.lang3.EnumUtils;
import org.apache.poi.hpsf.Decimal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -248,6 +247,54 @@ public ResponseEntity updateTheRemissionAmount(String paymentReference, Resubmit
}
return new ResponseEntity<>(null, HttpStatus.OK);
}

public RefundListDtoResponse getRefundsApprovedFromRefundService (String ccdCaseNumber, MultiValueMap<String, String> headers) {
final var refundListDtoResponse = getRefundsForCalculations(ccdCaseNumber, headers);
if (refundListDtoResponse == null){
return RefundListDtoResponse.buildRefundListWith().refundList(new ArrayList<>()).build();
}
return RefundListDtoResponse.buildRefundListWith()
.refundList(refundListDtoResponse.getRefundList()).build();
}

private RefundListDtoResponse getRefundsForCalculations(String ccdCaseNumber, MultiValueMap<String, String> headers) {

RefundListDtoResponse refundListDtoResponse = null;

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(refundApiUrl + REFUND_ENDPOINT).queryParam("ccdCaseNumber",ccdCaseNumber);

LOG.info("builder.toUriString() : {}", builder.toUriString());

try {

LOG.info("restTemplateRefundsGroup : {}", restTemplateRefundsGroup);

// call refund app
ResponseEntity<RefundListDtoResponse> refundListDtoResponseEntity = restTemplateRefundsGroup
.exchange(builder.toUriString(), HttpMethod.GET, createEntity(headers), RefundListDtoResponse.class);

return refundListDtoResponseEntity.hasBody() ? refundListDtoResponseEntity.getBody() : null;

} catch (HttpClientErrorException httpClientErrorException) {

if (httpClientErrorException.getStatusCode().equals(HttpStatus.BAD_REQUEST)
&& httpClientErrorException.getMessage().equals("Refund list is empty for given criteria")) {
LOG.info("The refund list is empty.", httpClientErrorException);
return RefundListDtoResponse.buildRefundListWith().refundList(new ArrayList<>()).build();
} else {
LOG.error("Client err ", httpClientErrorException);
}
} catch (Exception exception) {
exception.printStackTrace();
if (exception.getCause() != null) {
LOG.info("exception.getCause(): " + exception.getCause());
LOG.info("exception.getCause().getMessage(): " + exception.getCause().getMessage());
}
LOG.error("Client err ", exception);
}
return refundListDtoResponse;

}
private RefundListDtoResponse getRefundsFromRefundService(String ccdCaseNumber, MultiValueMap<String, String> headers) {


Expand Down Expand Up @@ -717,7 +764,8 @@ else if (!refundedFees.stream().flatMap(List::stream).anyMatch(fee.getId().toStr

refundListDtoResponse.getRefundList().forEach(refundDto -> {

if (refundDto.getPaymentReference().equals(paymentDto1.getReference())) {
if (refundDto.getPaymentReference().equals(paymentDto1.getReference()) &&
isAnAcceptedRefund(refundDto)) {
paymentDto1.setOverPayment(BigDecimal.ZERO);
}
});
Expand Down Expand Up @@ -747,7 +795,8 @@ else if (!refundedFees.stream().flatMap(List::stream).anyMatch(fee.getId().toStr
refundListDtoResponse.getRefundList().forEach(refundDto -> {

if (refundDto.getCcdCaseNumber().equals(feeDto.getCcdCaseNumber()) &&
(Arrays.stream(refundDto.getFeeIds().split(",")).anyMatch(feeDto.getId().toString()::equals))
(Arrays.stream(refundDto.getFeeIds().split(",")).anyMatch(feeDto.getId().toString()::equals)) &&
isAnAcceptedRefund(refundDto)
) {
feeDto.setOverPayment(BigDecimal.ZERO);
}
Expand All @@ -771,6 +820,9 @@ else if (!refundedFees.stream().flatMap(List::stream).anyMatch(fee.getId().toStr
return paymentGroupDto;
}

private boolean isAnAcceptedRefund(RefundDto refundDto){
return refundDto.getRefundStatus().getName().equals("Accepted");
}

public PaymentGroupResponse checkRefundAgainstRemissionV2(MultiValueMap<String, String> headers,
PaymentGroupResponse paymentGroupResponse, String ccdCaseNumber) {
Expand Down Expand Up @@ -900,7 +952,7 @@ else if (!refundedFees.stream().flatMap(List::stream).anyMatch(fee.getId().toStr
if (refundListDtoResponse != null) {
refundListDtoResponse.getRefundList().forEach(refundDto -> {

if (refundDto.getCcdCaseNumber().equals(feeDto.getCcdCaseNumber())
if (refundDto.getCcdCaseNumber().equals(feeDto.getCcdCaseNumber()) && isAnAcceptedRefund(refundDto)
) {
feeDto.setOverPayment(BigDecimal.ZERO);
}
Expand All @@ -913,7 +965,8 @@ else if (!refundedFees.stream().flatMap(List::stream).anyMatch(fee.getId().toStr

refundListDtoResponse.getRefundList().forEach(refundDto -> {

if (refundDto.getPaymentReference().equals(paymentDto.getReference())) {
if (refundDto.getPaymentReference().equals(paymentDto.getReference()) &&
isAnAcceptedRefund(refundDto)) {
paymentDto.setOverPayment(BigDecimal.ZERO);
}
});
Expand Down

0 comments on commit f7dc134

Please sign in to comment.