diff --git a/src/main/java/com/iemr/flw/controller/IncentiveController.java b/src/main/java/com/iemr/flw/controller/IncentiveController.java index 32a5c16f..a251327d 100644 --- a/src/main/java/com/iemr/flw/controller/IncentiveController.java +++ b/src/main/java/com/iemr/flw/controller/IncentiveController.java @@ -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; @@ -31,6 +32,9 @@ public class IncentiveController { @Autowired IncentiveService incentiveService; + private static final Gson gson = new Gson(); + + @Autowired private JwtUtil jwtUtil; @@ -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) diff --git a/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java b/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java index 9cb35662..25a0b652 100644 --- a/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java +++ b/src/main/java/com/iemr/flw/repo/iemr/BenReferDetailsRepo.java @@ -51,8 +51,8 @@ public interface BenReferDetailsRepo extends JpaRepository lockMap = new ConcurrentHashMap<>(); @Override public String registerHBYC(List hbycDTOs) { @@ -286,6 +290,7 @@ public List getHBNCDetails(GetBenRequestHandler dto) { responseDTO.setFields(fields); result.add(responseDTO); + } page++; @@ -1004,12 +1009,32 @@ private void checkAndAddHbncIncentives(List 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 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); + } + } } + } @@ -1019,6 +1044,80 @@ private void checkAndAddHbncIncentives(List hbncVisits, Integer userI } + private void generateHighRiskPostpartumIncentives(Long motherBenId, boolean childHbnc42Done, Integer userId) { + + if (!childHbnc42Done) return; + + List 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 vaccinationList) { String createdBy = vaccinationList.get(0).getCreatedBy(); diff --git a/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java index 08caccd0..4cbfeb87 100644 --- a/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/DiseaseControlServiceImpl.java @@ -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; @@ -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; } @@ -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 getAllLeprosyData(String createdBy) { logger.info("Fetching leprosy data for createdBy: " + createdBy); diff --git a/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java index f8e72fdc..b89cb137 100644 --- a/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/IncentiveServiceImpl.java @@ -1,13 +1,11 @@ package com.iemr.flw.service.impl; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.iemr.flw.domain.iemr.*; import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.GroupName; -import com.iemr.flw.masterEnum.IncentiveApprovalStatus; import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; @@ -16,7 +14,6 @@ import com.iemr.flw.service.NotificationService; import com.iemr.flw.service.UserService; import com.iemr.flw.utils.JwtUtil; -import jakarta.persistence.criteria.CriteriaBuilder; import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -227,7 +224,7 @@ public String getAllIncentivesByUserId(GetBenRequestHandler request) { try { addIncentiveForIronTablets(request.getAshaId()); - incentiveOfNcdReferal(request.getAshaId(), request.getVillageID()); + incentiveOfNcdReferal(request.getAshaId(), request.getVillageID(),request); } catch (Exception e) { logger.error("Error in incentiveOfNcdReferal: ", e); } @@ -945,7 +942,7 @@ public String updateClaimStatus(Integer ashaId, Integer month, Integer year, Boo } } - private void incentiveOfNcdReferal(Integer ashaId, Integer stateId) { + private void incentiveOfNcdReferal(Integer ashaId, Integer stateId, GetBenRequestHandler request) { try { String userName = userRepo.getUserNamedByUserId(ashaId); String groupName = resolveGroupName(stateId); @@ -980,21 +977,23 @@ private void incentiveOfNcdReferal(Integer ashaId, Integer stateId) { } if (hwcReferralEnumeration != null) { - logger.info("hwcReferralEnumeration :" + hwcReferralEnumeration.getDescription()); + logger.info("hwcReferralEnumeration: {}", hwcReferralEnumeration.getDescription()); logger.info("userName={}", userName); - LocalDate now = LocalDate.now(); + LocalDate requestDate = request.getFromDate() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); Long referralCount = benReferDetailsRepo.countMonthlyReferrals( userName, - now.getMonthValue(), - now.getYear()); - - logger.info("referralCount :" + referralCount); + requestDate.getMonthValue(), + requestDate.getYear() + ); + logger.info("referralCount: {}", referralCount); if (referralCount >= 10) { - addHwcReferalAshaIncentiveRecord( hwcReferralEnumeration, ashaId, diff --git a/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java index c4dab2c9..62534f07 100644 --- a/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java @@ -784,28 +784,39 @@ private void checkAndAddAntaraIncentive(PNCVisit ect) { } - if (ect.getMotherDangerSign() != null - && !ect.getMotherDangerSign().isEmpty() - && ect.getPncPeriod() == 42) { - IncentiveActivity highRiskPostpartumCareActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("HIGH_RISK_POSTPARTUM_CARE", GroupName.ACTIVITY.getDisplayName()); - if (highRiskPostpartumCareActivityCH != null) { - addIncenticeRecord(ect, userId, highRiskPostpartumCareActivityCH); - - } - - } - if (ect.getPncPeriod() == 42) { - IncentiveActivity highRiskPostpartumHealthCareActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("HIGH_RISK_POSTPARTUM_HEALTH_CHECK", GroupName.ACTIVITY.getDisplayName()); - - if (highRiskPostpartumHealthCareActivityCH != null) { - addIncenticeRecord(ect, userId, highRiskPostpartumHealthCareActivityCH); - - } - - - } +// if (ect.getMotherDangerSign() != null +// && !ect.getMotherDangerSign().isEmpty() +// && ect.getPncPeriod() != null +// && ect.getPncPeriod() >= 1 +// && ect.getPncPeriod() <= 42) { +// IncentiveActivity highRiskPostpartumCareActivityCH = +// incentivesRepo.findIncentiveMasterByNameAndGroup("HIGH_RISK_POSTPARTUM_CARE", GroupName.ACTIVITY.getDisplayName()); +// +// if (highRiskPostpartumCareActivityCH != null ) { +// addIncenticeRecord(ect, userId, highRiskPostpartumCareActivityCH); +// } +// +// } +// if (ect.getPncPeriod() == 42 +// && !ect.getMotherDeath() +// && ancVisitRepo.findByBenId(ect.getBenId()) +// .stream() +// .filter(ancVisit -> ancVisit.getAncVisit() != null +// && ancVisit.getAncVisit() >= 1 +// && ancVisit.getAncVisit() <= 7) +// .anyMatch(ancVisit -> +// Boolean.TRUE.equals(ancVisit.getIsHrpConfirmed()))) { +// +// IncentiveActivity highRiskPostpartumHealthCareActivityCH = +// incentivesRepo.findIncentiveMasterByNameAndGroup( +// "HIGH_RISK_POSTPARTUM_HEALTH_CHECK", +// GroupName.ACTIVITY.getDisplayName()); +// +// if (highRiskPostpartumHealthCareActivityCH != null) { +// addIncenticeRecord(ect, userId, +// highRiskPostpartumHealthCareActivityCH); +// } +// } if (ect.getPncPeriod() == 1 || ect.getPncPeriod() == 3 || ect.getPncPeriod() == 7) { if(ect.getContraceptionMethod()!=null){ if ((ect.getContraceptionMethod().equals("POST PARTUM STERILIZATION (PPS)") @@ -829,6 +840,7 @@ private void checkAndAddAntaraIncentive(PNCVisit ect) { } + @Transactional private void addIncenticeRecord(PNCVisit ect, Integer userId, IncentiveActivity antaraActivity) { diff --git a/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java b/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java index 1bbc5ec0..2dc71c08 100644 --- a/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/TBConfirmedCaseServiceImpl.java @@ -25,6 +25,7 @@ import java.sql.Timestamp; import java.time.LocalDate; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @@ -149,12 +150,28 @@ private void checkIncentive(TBConfirmedCase entity){ if (isDrTb) { - incentiveLogicService.incentiveForTbFollowUpIsDrTb( - entity.getBenId(), - Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), - Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), - entity.getUserId() - ); + if("Shorter Regimen (9–12 Months)".equalsIgnoreCase(regimen)){ + if(isEligibleForIncentive(entity.getTreatmentStartDate(),entity.getExpectedTreatmentCompletionDate(),9)){ + incentiveLogicService.incentiveForTbFollowUpIsDrTb( + entity.getBenId(), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + entity.getUserId() + ); + } + + }else if("Longer Regimen (18–24 Months)".equalsIgnoreCase(regimen)){ + if(isEligibleForIncentive(entity.getTreatmentStartDate(),entity.getExpectedTreatmentCompletionDate(),18)) { + + incentiveLogicService.incentiveForTbFollowUpIsDrTb( + entity.getBenId(), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + Timestamp.valueOf(entity.getExpectedTreatmentCompletionDate().atStartOfDay()), + entity.getUserId() + ); + } + } + } else { incentiveLogicService.incentiveForTbFollowUp( entity.getBenId(), @@ -168,6 +185,24 @@ private void checkIncentive(TBConfirmedCase entity){ } } + public boolean isEligibleForIncentive(LocalDate treatmentStartDate, LocalDate endDate,Integer minMonth) { + + if (treatmentStartDate == null || endDate == null) { + return false; + } + + LocalDate start = treatmentStartDate.atStartOfDay() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + + LocalDate end = endDate.atStartOfDay() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + + return end.isAfter(start.plusMonths(minMonth)) + || end.isEqual(start.plusMonths(minMonth)); + } + @Override public String getByBenId(Long benId, String authorisation) throws Exception { OutputResponse response = new OutputResponse();