Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #964 from egovernments/ISTE-345
Browse files Browse the repository at this point in the history
ISTE-345: Added chnages to remove multiple call to fetch bill API .
  • Loading branch information
pradeepkumarcm-egov authored Oct 8, 2024
2 parents a477f0a + 9664d0d commit cdc7d7c
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,33 @@
import java.math.BigInteger;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.validation.Valid;
import io.swagger.models.auth.In;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.egov.common.contract.request.RequestInfo;
import org.egov.common.contract.request.User;
import org.egov.mdms.model.MasterDetail;
import org.egov.mdms.model.MdmsCriteria;
import org.egov.mdms.model.MdmsCriteriaReq;
import org.egov.mdms.model.ModuleDetail;
import org.egov.tracer.model.CustomException;
import org.egov.wscalculation.config.WSCalculationConfiguration;
Expand Down Expand Up @@ -296,29 +288,32 @@ private List<Demand> createDemand(RequestInfo requestInfo, List<Calculation> cal


if (!isWSUpdateSMS) {
List<String> billNumbers = fetchBill(demands, waterConnectionRequest.getRequestInfo());
Long billDate = fetchBillDate(demands,waterConnectionRequest.getRequestInfo());
billDate =billDate + 1296000000l;
LocalDate billDateLocal = Instant.ofEpochMilli(billDate).atZone(ZoneId.systemDefault()).toLocalDate();
String paymentDueDate = billDateLocal.format(dateTimeFormatter);
if(isOnlinePaymentAllowed(requestInfo,tenantId)) {
if(fetchTotalBillAmount(demands,requestInfo).signum()> 0) {
sendPaymentSMSNotification(requestInfo,tenantId,owner,waterConnectionRequest,property,demandDetails,consumerCode,demands,isForConnectionNO,businessService,billCycle,billNumbers,paymentDueDate);
//sendPaymentAndBillSMSNotification(requestInfo,tenantId,owner,waterConnectionRequest,property,demandDetails,consumerCode,demands,isForConnectionNO,businessService,billCycle,billNumbers,paymentDueDate);
List<BillV2> bills = fetchBill(demands, waterConnectionRequest.getRequestInfo());
if (!CollectionUtils.isEmpty(bills)) {
String billNumber = bills.get(0).getBillNumber();
Long billDate = bills.get(0).getBillDate();
billDate = billDate + 1296000000l;
LocalDate billDateLocal = Instant.ofEpochMilli(billDate).atZone(ZoneId.systemDefault()).toLocalDate();
String paymentDueDate = billDateLocal.format(dateTimeFormatter);
BigDecimal totalAmount=bills.get(0).getTotalAmount();
if (ObjectUtils.isEmpty(totalAmount) && totalAmount.signum() > 0) {
if (isOnlinePaymentAllowed(requestInfo, tenantId)) {
sendPaymentSMSNotification(requestInfo, tenantId, owner, waterConnectionRequest, property, demandDetails, consumerCode, demands, isForConnectionNO, businessService, billCycle, billNumber, paymentDueDate,totalAmount);
//sendPaymentAndBillSMSNotification(requestInfo,tenantId,owner,waterConnectionRequest,property,demandDetails,consumerCode,demands,isForConnectionNO,businessService,billCycle,billNumbers,paymentDueDate,totalAmount);
}
sendDownloadBillSMSNotification(requestInfo, tenantId, owner, waterConnectionRequest, property, demandDetails, consumerCode, demands, isForConnectionNO, businessService, billCycle, billNumber, paymentDueDate, totalAmount);
}
}
BigDecimal totalAmount=fetchTotalBillAmount(demands,requestInfo);
//log.info("Total Amount from fetch Bill"+String.valueOf(totalAmount));
if(totalAmount!=null && totalAmount.signum()> 0)
sendDownloadBillSMSNotification(requestInfo,tenantId,owner,waterConnectionRequest,property,demandDetails,consumerCode,demands,isForConnectionNO,businessService,billCycle,billNumbers,paymentDueDate,totalAmount);
}
}


return finalDemandRes;
}

private void sendPaymentSMSNotification(RequestInfo requestInfo, String tenantId, User owner, WaterConnectionRequest waterConnectionRequest, Property property, List<DemandDetail> demandDetails, String consumerCode, List<Demand> demands, Boolean isForConnectionNO, String businessService, String billCycle, List<String> billNumbers, String paymentDueDate ) {
private void sendPaymentSMSNotification(RequestInfo requestInfo, String tenantId, User owner, WaterConnectionRequest waterConnectionRequest,
Property property, List<DemandDetail> demandDetails, String consumerCode, List<Demand> demands, Boolean isForConnectionNO,
String businessService, String billCycle, String billNumber, String paymentDueDate,BigDecimal totalAmount ) {
HashMap<String, String> localizationMessage = util.getLocalizationMessage(requestInfo,
WSCalculationConstant.mGram_Consumer_Payment, tenantId);
String connectionType = null;
Expand All @@ -335,7 +330,6 @@ private void sendPaymentSMSNotification(RequestInfo requestInfo, String tenantId


String messageString = localizationMessage.get(WSCalculationConstant.MSG_KEY);
BigDecimal totalAmount = fetchTotalBillAmount(demands,requestInfo);

if (!StringUtils.isEmpty(messageString) && isForConnectionNO) {
messageString = messageString.replace("{ownername}", owner.getName());
Expand All @@ -352,7 +346,7 @@ private void sendPaymentSMSNotification(RequestInfo requestInfo, String tenantId
}
}
}
private void sendDownloadBillSMSNotification(RequestInfo requestInfo, String tenantId, User owner, WaterConnectionRequest waterConnectionRequest, Property property, List<DemandDetail> demandDetails, String consumerCode, List<Demand> demands, Boolean isForConnectionNO, String businessService, String billCycle,List<String> billNumbers, String paymentDueDate,BigDecimal totalamount) {
private void sendDownloadBillSMSNotification(RequestInfo requestInfo, String tenantId, User owner, WaterConnectionRequest waterConnectionRequest, Property property, List<DemandDetail> demandDetails, String consumerCode, List<Demand> demands, Boolean isForConnectionNO, String businessService, String billCycle,String billNumber, String paymentDueDate,BigDecimal totalamount) {
HashMap<String, String> localizationMessage = util.getLocalizationMessage(requestInfo,
WSCalculationConstant.mGram_Consumer_NewBill, tenantId);
String actionLink = config.getNotificationUrl()
Expand All @@ -371,8 +365,8 @@ private void sendDownloadBillSMSNotification(RequestInfo requestInfo, String ten
//System.out.println("Localization message get bill::" + messageString);
//System.out.println("isForConnectionNO:" + isForConnectionNO);
if (!StringUtils.isEmpty(messageString) && isForConnectionNO) {
if (totalamount!=null && billNumbers.size() > 0 && totalamount.signum()>0) {
actionLink = actionLink.replace("$billNumber", billNumbers.get(0));
if (totalamount!=null && totalamount.signum()>0) {
actionLink = actionLink.replace("$billNumber", billNumber);
messageString = messageString.replace("{ownername}", owner.getName());
messageString = messageString.replace("{Period}", billCycle);
messageString = messageString.replace("{consumerno}", consumerCode);
Expand Down Expand Up @@ -970,7 +964,7 @@ private List<Demand> updateDemandForCalculation(RequestInfo requestInfo, List<Ca
}
demandRes = demandRepository.updateDemand(requestInfo, demands);
finalDemandRes.addAll(demandRes);
List<String> billNumbers = fetchBill(demands, waterConnectionRequest.getRequestInfo());
List<BillV2> bills = fetchBill(demands, waterConnectionRequest.getRequestInfo());
// Long billDate = fetchBillDate(demands,waterConnectionRequest.getRequestInfo());
// billDate =billDate + 1296000000l;
// LocalDate billDateLocal = Instant.ofEpochMilli(billDate).atZone(ZoneId.systemDefault()).toLocalDate();
Expand Down Expand Up @@ -1290,26 +1284,31 @@ private boolean isCurrentDateIsMatching(String billingFrequency, long dayOfMonth
return true;
}

public List<String> fetchBill(List<Demand> demandResponse, RequestInfo requestInfo) {
boolean notificationSent = false;
List<String> billNumber = new ArrayList<>();
public List<BillV2> fetchBill(List<Demand> demandResponse, RequestInfo requestInfo) {
boolean notificationSent = false; new ArrayList<>();
List<BillV2> bills =null;
for (Demand demand : demandResponse) {
try {
Object result = serviceRequestRepository.fetchResult(
calculatorUtils.getFetchBillURL(demand.getTenantId(), demand.getConsumerCode()),
RequestInfoWrapper.builder().requestInfo(requestInfo).build());
billNumber = JsonPath.read(result, "$.Bill.*.billNumber");
//billNumber = JsonPath.read(result, "$.Bill.*.billNumber");
HashMap<String, Object> billResponse = new HashMap<>();

billResponse.put("requestInfo", requestInfo);
billResponse.put("billResponse", result);
try {
bills = mapper.convertValue(result, BillResponseV2.class).getBill();
} catch (IllegalArgumentException e) {
throw new CustomException("PARSING_ERROR", "Failed to parse response from Demand Search");
}
wsCalculationProducer.push(configs.getPayTriggers(), billResponse);
notificationSent = true;
} catch (Exception ex) {
log.error("Fetch Bill Error", ex);
}
}
return billNumber;
return bills;
}

public Long fetchBillDate(List<Demand> demandResponse, RequestInfo requestInfo) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.egov.wscalculation.web.models;

public class BillDetailV2 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.egov.wscalculation.web.models;

import java.util.ArrayList;
import java.util.List;
import org.egov.common.contract.response.ResponseInfo;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* BillResponse
*/

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BillResponseV2 {

@JsonProperty("ResposneInfo")
private ResponseInfo resposneInfo = null;

@JsonProperty("Bill")
private List<BillV2> bill = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.egov.wscalculation.web.models;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.JsonNode;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BillV2 {

@JsonProperty("id")
@Size(max = 256)
private String id;

@JsonProperty("userId")
private String userId;

@JsonProperty("mobileNumber")
@Pattern(regexp = "^[0-9]{10}$", message = "MobileNumber should be 10 digit number")
private String mobileNumber;

@JsonProperty("payerName")
@Size(max = 256)
private String payerName;

@JsonProperty("payerAddress")
@Size(max = 1024)
private String payerAddress;

@JsonProperty("payerEmail")
@Size(max = 256)
private String payerEmail;

@JsonProperty("status")
private BillStatus status;

@JsonProperty("totalAmount")
private BigDecimal totalAmount;

@JsonProperty("businessService")
@Size(max = 256)
private String businessService;

@JsonProperty("billNumber")
@Size(max = 256)
private String billNumber;

@JsonProperty("billDate")
private Long billDate;

@JsonProperty("consumerCode")
@Size(max = 256)
private String consumerCode;

@JsonProperty("additionalDetails")
private JsonNode additionalDetails;

@JsonProperty("billDetails")
@Valid
private List<BillDetailV2> billDetails;

@JsonProperty("tenantId")
@Size(max = 256)
private String tenantId;

@JsonProperty("fileStoreId")
private String fileStoreId;

@JsonProperty("auditDetails")
private AuditDetails auditDetails;

/**
* status of the bill .
*/
public enum BillStatus {

ACTIVE("ACTIVE"),

CANCELLED("CANCELLED"),

PAID("PAID"),

PARTIALLY_PAID("PARTIALLY_PAID"),

PAYMENT_CANCELLED("PAYMENT_CANCELLED"),

EXPIRED("EXPIRED");

private String value;

BillStatus(String value) {
this.value = value;
}

@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static BillStatus fromValue(String text) {
for (BillStatus b : BillStatus.values()) {
if (String.valueOf(b.value).equalsIgnoreCase(text)) {
return b;
}
}
return null;
}
}

public BillV2 addBillDetailsItem(BillDetailV2 billDetailsItem) {
if (this.billDetails == null) {
this.billDetails = new ArrayList<>();
}
this.billDetails.add(billDetailsItem);
return this;
}
}

0 comments on commit cdc7d7c

Please sign in to comment.