Skip to content
10 changes: 8 additions & 2 deletions src/main/java/com/iemr/flw/controller/IncentiveController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iemr.flw.controller;

import com.google.gson.Gson;
import com.iemr.flw.dto.identity.GetBenRequestHandler;
import com.iemr.flw.dto.iemr.*;
import com.iemr.flw.service.IncentiveService;
Expand Down Expand Up @@ -31,6 +32,9 @@ public class IncentiveController {
@Autowired
IncentiveService incentiveService;

private static final Gson gson = new Gson();


@Autowired
private JwtUtil jwtUtil;

Expand Down Expand Up @@ -91,8 +95,10 @@ public String getAllIncentivesByUserId(@RequestBody GetBenRequestHandler request
try {

if (requestDTO != null) {
logger.info("request object with timestamp : " + new Timestamp(System.currentTimeMillis()) + " "
+ requestDTO);

logger.info("Request object with timestamp: {} | Request JSON: {}",
new Timestamp(System.currentTimeMillis()),
gson.toJson(requestDTO));
String s = incentiveService.getAllIncentivesByUserId(requestDTO);
//logger.info("User Incentive:" + s);
if (s != null)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public interface BenReferDetailsRepo extends JpaRepository<BenReferDetails, Long
SELECT COUNT(*)
FROM db_iemr.t_benreferdetails
WHERE CreatedBy = :userName
AND MONTH(CreatedDate) = :month
AND YEAR(CreatedDate) = :year
AND MONTH(revisitDate) = :month
AND YEAR(revisitDate) = :year
""", nativeQuery = true)
Long countMonthlyReferrals(
@Param("userName") String userName,
Expand Down
32 changes: 31 additions & 1 deletion src/main/java/com/iemr/flw/service/impl/CampaignServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private void checkMonthlyPulsePolioIncentive(Integer ashaId,LocalDate startDate,
private void checkMonthlyFilariasisIncentive(Integer ashaId,LocalDate startDate,LocalDate endDate) {
IncentiveActivity FILARIASIS_MEDICINE_DISTRIBUTION = incentivesRepo.findIncentiveMasterByNameAndGroup("FILARIASIS_MEDICINE_DISTRIBUTION", GroupName.ACTIVITY.getDisplayName());
if (FILARIASIS_MEDICINE_DISTRIBUTION != null) {
addAshaIncentiveRecord(FILARIASIS_MEDICINE_DISTRIBUTION, ashaId,startDate,endDate);
addAshaIncentiveRecordMda(FILARIASIS_MEDICINE_DISTRIBUTION, ashaId,startDate,endDate);
}

}
Expand Down Expand Up @@ -502,5 +502,35 @@ record = new IncentiveActivityRecord();
}
}


private void addAshaIncentiveRecordMda(IncentiveActivity incentiveActivity, Integer ashaId,LocalDate startDate,LocalDate endDate) {

Timestamp startOfMonth = Timestamp.valueOf(startDate.atStartOfDay());
Timestamp endOfMonth = Timestamp.valueOf(endDate.atStartOfDay());

IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId(
incentiveActivity.getId(),
endOfMonth,
0L,ashaId

);


if (record == null) {
record = new IncentiveActivityRecord();
record.setActivityId(incentiveActivity.getId());
record.setCreatedDate(endOfMonth);
record.setCreatedBy(userServiceRoleRepo.getUserNamedByUserId(ashaId));
record.setStartDate(endOfMonth);
record.setEndDate(endOfMonth);
record.setUpdatedDate(endOfMonth);
record.setUpdatedBy(userServiceRoleRepo.getUserNamedByUserId(ashaId));
record.setBenId(0L);
record.setAshaId(ashaId);
record.setAmount(Long.valueOf(incentiveActivity.getRate()));
recordRepo.save(record);
}
}

}

103 changes: 101 additions & 2 deletions src/main/java/com/iemr/flw/service/impl/ChildCareServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.iemr.flw.domain.identity.RMNCHBeneficiaryDetailsRmnch;
import com.iemr.flw.domain.iemr.*;
import com.iemr.flw.dto.identity.GetBenRequestHandler;
import com.iemr.flw.dto.iemr.*;
Expand All @@ -16,6 +17,7 @@
import com.iemr.flw.service.UserService;
import com.iemr.flw.utils.JwtUtil;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
import org.aspectj.weaver.ast.Or;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
Expand All @@ -36,6 +38,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -98,6 +101,7 @@ public class ChildCareServiceImpl implements ChildCareService {
@Autowired
private PNCVisitRepo pncVisitRepo;

private final ConcurrentHashMap<String, Object> lockMap = new ConcurrentHashMap<>();

@Override
public String registerHBYC(List<HbycRequestDTO> hbycDTOs) {
Expand Down Expand Up @@ -286,6 +290,7 @@ public List<HbncVisitResponseDTO> getHBNCDetails(GetBenRequestHandler dto) {
responseDTO.setFields(fields);
result.add(responseDTO);


}
page++;

Expand Down Expand Up @@ -1004,12 +1009,32 @@ private void checkAndAddHbncIncentives(List<HbncVisit> hbncVisits, Integer userI
}

if (stateCode.equals(StateCode.CG.getStateCode())) {
if (hbncVisit.getVisit_day().equals("42nd Day")) {
IncentiveActivity visitActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("HBNC_0_42_DAYS", GroupName.ACTIVITY.getDisplayName());
if ("42nd Day".equals(hbncVisit.getVisit_day())) {
IncentiveActivity visitActivityCH = incentivesRepo
.findIncentiveMasterByNameAndGroup("HBNC_0_42_DAYS", GroupName.ACTIVITY.getDisplayName());
createIncentiveRecordforHbncVisit(hbncVisit, benId, visitActivityCH, "HBNC_0_42_DAYS");

// 1. Child ka record
List<RMNCHBeneficiaryDetailsRmnch> childList =
beneficiaryRepo.findByBenficieryid(hbncVisit.getBeneficiaryId());

if (childList != null && !childList.isEmpty()) {
RMNCHBeneficiaryDetailsRmnch child = childList.get(childList.size() - 1);

// 2. Same household se mother
RMNCHBeneficiaryDetailsRmnch mother = beneficiaryRepo
.findByHouseoldId(child.getHouseoldId()).stream()
.filter(b -> !"NewBorn".equalsIgnoreCase(b.getRegistrationType())
&& "Postnatal Mother".equalsIgnoreCase(b.getReproductiveStatus()))
.findFirst()
.orElse(null);

if (mother != null) {
generateHighRiskPostpartumIncentives(mother.getBenficieryid(), true, userId);
}
}
}

}


Expand All @@ -1019,6 +1044,80 @@ private void checkAndAddHbncIncentives(List<HbncVisit> hbncVisits, Integer userI
}


private void generateHighRiskPostpartumIncentives(Long motherBenId, boolean childHbnc42Done, Integer userId) {

if (!childHbnc42Done) return;

List<PNCVisit> motherPnc = pncVisitRepo.findByBenId(motherBenId);

PNCVisit visit42 = motherPnc.stream()
.filter(v -> Integer.valueOf(42).equals(v.getPncPeriod()))
.findFirst()
.orElse(null);

if (visit42 == null) return;

boolean hasDangerSign = motherPnc.stream()
.anyMatch(v -> v.getMotherDangerSign() != null
&& !v.getMotherDangerSign().isEmpty()
&& v.getPncPeriod() != null
&& v.getPncPeriod() >= 1
&& v.getPncPeriod() <= 42);

if (hasDangerSign) {
IncentiveActivity act1 = incentivesRepo.findIncentiveMasterByNameAndGroup(
"HIGH_RISK_POSTPARTUM_CARE", GroupName.ACTIVITY.getDisplayName());
if (act1 != null) {
addIncenticeRecord(visit42, userId, act1); // date 42 wale visit ki
}
}

boolean motherAlive = !Boolean.TRUE.equals(visit42.getMotherDeath());

boolean hrpConfirmed = ancVisitRepo.findByBenId(motherBenId).stream()
.filter(a -> a.getAncVisit() != null && a.getAncVisit() >= 1 && a.getAncVisit() <= 7)
.anyMatch(a -> Boolean.TRUE.equals(a.getIsHrpConfirmed()));

if (motherAlive && hrpConfirmed) {
IncentiveActivity act2 = incentivesRepo.findIncentiveMasterByNameAndGroup(
"HIGH_RISK_POSTPARTUM_HEALTH_CHECK", GroupName.ACTIVITY.getDisplayName());
if (act2 != null) {
addIncenticeRecord(visit42, userId, act2);
}
}
}

@Transactional
private void addIncenticeRecord(PNCVisit ect, Integer userId, IncentiveActivity antaraActivity) {

String lockKey = antaraActivity.getId() + "_" + ect.getBenId() + "_" + ect.getPncDate();

Object lock = lockMap.computeIfAbsent(lockKey, k -> new Object());

synchronized (lock) {
IncentiveActivityRecord record = recordRepo
.findRecordByActivityIdCreatedDateBenId(antaraActivity.getId(), ect.getPncDate(), ect.getBenId(),userId);

if (record == null) {
record = new IncentiveActivityRecord();
record.setActivityId(antaraActivity.getId());
record.setCreatedDate(ect.getPncDate());
record.setCreatedBy(ect.getCreatedBy());
record.setStartDate(ect.getPncDate());
record.setEndDate(ect.getPncDate());
record.setUpdatedDate(ect.getPncDate());
record.setUpdatedBy(ect.getCreatedBy());
record.setBenId(ect.getBenId());
record.setAshaId(userId);
record.setAmount(Long.valueOf(antaraActivity.getRate()));
recordRepo.save(record);
}
}

lockMap.remove(lockKey, lock);
}


private void checkAndAddIncentives(List<ChildVaccination> vaccinationList) {
String createdBy = vaccinationList.get(0).getCreatedBy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -352,41 +353,51 @@ private LeprosyFollowUp saveLeprosyFollowUpData(LeprosyFollowUpDTO data) {
entity.setLastModDate(
data.getLastModDate() != null ? data.getLastModDate() : new Timestamp(System.currentTimeMillis()));
if(!leprosyFollowUpRepository.findByBenId(entity.getBenId()).isEmpty()){
if(leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()>=6 && leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()<=12){
Integer userId = userRepo.getUserIdByName(entity.getCreatedBy());
IncentiveActivityRecord incentiveActivityRecord =
incentiveLogicService.incentiveForLeprosyPaucibacillaryConfirmed(
entity.getBenId(),
entity.getTreatmentEndDate(),
entity.getTreatmentEndDate(),
userId);

if (incentiveActivityRecord != null) {
logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}",
incentiveActivityRecord.getId());
} else {
logger.info("Incentive not created");
if(entity.getTypeOfLeprosy().equals("PB (Paucibacillary)")){
if(leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()>=6 && leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()<=12){
Integer userId = userRepo.getUserIdByName(entity.getCreatedBy());
IncentiveActivityRecord incentiveActivityRecord =
incentiveLogicService.incentiveForLeprosyPaucibacillaryConfirmed(
entity.getBenId(),
entity.getTreatmentEndDate(),
entity.getTreatmentEndDate(),
userId);

if (incentiveActivityRecord != null) {
logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}",
incentiveActivityRecord.getId());
} else {
logger.info("Incentive not created");
}
}
}

}

if(!leprosyFollowUpRepository.findByBenId(entity.getBenId()).isEmpty()){
if(leprosyFollowUpRepository.findByBenId(entity.getBenId()).size()>=12){
Integer userId = userRepo.getUserIdByName(entity.getCreatedBy());
IncentiveActivityRecord incentiveActivityRecord =
incentiveLogicService.incentiveForLeprosyMultibacillaryConfirmed(
entity.getBenId(),
entity.getTreatmentEndDate(),
entity.getTreatmentEndDate(),
userId);

if (incentiveActivityRecord != null) {
logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}",
incentiveActivityRecord.getId());
} else {
logger.info("Incentive not created");
if(entity.getTypeOfLeprosy().equals("MB (Multibacillary)")) {
if (entity.getTreatmentStartDate() != null && entity.getTreatmentEndDate() != null) {
if (isEligibleForIncentive(entity.getTreatmentStartDate(), entity.getTreatmentEndDate())) {
Integer userId = userRepo.getUserIdByName(entity.getCreatedBy());
IncentiveActivityRecord incentiveActivityRecord =
incentiveLogicService.incentiveForLeprosyMultibacillaryConfirmed(
entity.getBenId(),
entity.getTreatmentEndDate(),
entity.getTreatmentEndDate(),
userId);

if (incentiveActivityRecord != null) {
logger.info("Incentive processed for Screening Leprosy successfully. RecordId={}",
incentiveActivityRecord.getId());
} else {
logger.info("Incentive not created");
}
}
}
}



}
return entity;
}
Expand All @@ -401,6 +412,24 @@ public String saveLeprosyFollowUp(LeprosyFollowUpDTO dto) {

}

public boolean isEligibleForIncentive(Date treatmentStartDate, Date endDate) {

if (treatmentStartDate == null || endDate == null) {
return false;
}

LocalDate start = treatmentStartDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();

LocalDate end = endDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();

return end.isAfter(start.plusMonths(12))
|| end.isEqual(start.plusMonths(12));
}

@Override
public List<DiseaseGetLeprosyDTO> getAllLeprosyData(String createdBy) {
logger.info("Fetching leprosy data for createdBy: " + createdBy);
Expand Down
Loading
Loading