diff --git a/src/main/java/com/iemr/flw/controller/MaternalHealthController.java b/src/main/java/com/iemr/flw/controller/MaternalHealthController.java index f0955af6..4a8dd64b 100644 --- a/src/main/java/com/iemr/flw/controller/MaternalHealthController.java +++ b/src/main/java/com/iemr/flw/controller/MaternalHealthController.java @@ -98,16 +98,19 @@ public String getPregnantWomanList(@RequestBody GetBenRequestHandler requestDTO, @Operation(summary = "save anc visit details") @RequestMapping(value = { "/ancVisit/saveAll" }, method = { RequestMethod.POST }) public String saveANCVisit(@RequestBody List ancVisitDTOs, - @RequestHeader(value = "Authorization") String Authorization) { + @RequestHeader(value = "jwtToken") String token) { OutputResponse response = new OutputResponse(); try { if (ancVisitDTOs.size() != 0) { logger.info("Saving ANC visits with timestamp : " + new Timestamp(System.currentTimeMillis())); - String s = maternalHealthService.saveANCVisit(ancVisitDTOs); - if (s != null) - response.setResponse(s); - else - response.setError(5000, "Saving anc data to db failed"); + if(token!=null){ + String s = maternalHealthService.saveANCVisit(ancVisitDTOs,jwtUtil.extractUserId(token)); + if (s != null) + response.setResponse(s); + else + response.setError(5000, "Saving anc data to db failed"); + } + } else response.setError(5000, "Invalid/NULL request obj"); } catch (Exception e) { diff --git a/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java b/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java index 0f078f5f..6f3143e0 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java +++ b/src/main/java/com/iemr/flw/domain/iemr/IRSRound.java @@ -28,4 +28,10 @@ public class IRSRound { @Column(name = "householdId") private Long householdId; + @Column(name = "created_by") + private String createdBy; + + @Column(name = "user_id") + private Integer userId; + } \ No newline at end of file diff --git a/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java b/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java index a943affe..e0f84e10 100644 --- a/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java +++ b/src/main/java/com/iemr/flw/domain/iemr/ScreeningLeprosy.java @@ -83,19 +83,19 @@ public class ScreeningLeprosy { @Column(name = "beneficiary_statusId") private Integer beneficiaryStatusId; - @Column(name = "beneficiary_status", length = 50) + @Column(name = "beneficiary_status", length = 255) private String beneficiaryStatus; @Column(name = "date_of_death") private Date dateOfDeath; - @Column(name = "place_of_death", length = 50) + @Column(name = "place_of_death", length = 255) private String placeOfDeath; @Column(name = "other_place_of_death", columnDefinition = "TEXT") private String otherPlaceOfDeath; - @Column(name = "reason_for_death", length = 50) + @Column(name = "reason_for_death", length = 255) private String reasonForDeath; @Column(name = "other_reason_for_death", columnDefinition = "TEXT") @@ -113,7 +113,7 @@ public class ScreeningLeprosy { @Column(name = "current_visit_number") private Integer currentVisitNumber; - @Column(name = "visit_label", length = 50) + @Column(name = "visit_label", length = 255) private String visitLabel; @Column(name = "visit_number") @@ -122,7 +122,7 @@ public class ScreeningLeprosy { @Column(name = "is_confirmed") private Boolean isConfirmed; - @Column(name = "leprosy_state", length = 50) + @Column(name = "leprosy_state", length = 255) private String leprosyState; @Temporal(TemporalType.DATE) @@ -178,40 +178,40 @@ public class ScreeningLeprosy { @Column(name = "weakness_feet_id") private Integer weaknessFeetId; - @Column(name = "recurrent_ulceration", length = 10) + @Column(name = "recurrent_ulceration", length = 255) private String recurrentUlceration; - @Column(name = "recurrent_tingling", length = 10) + @Column(name = "recurrent_tingling", length = 255) private String recurrentTingling; - @Column(name = "hypopigmented_patch", length = 50) + @Column(name = "hypopigmented_patch", length = 255) private String hypopigmentedPatch; - @Column(name = "thickened_skin", length = 10) + @Column(name = "thickened_skin", length = 255) private String thickenedSkin; - @Column(name = "skin_nodules", length = 10) + @Column(name = "skin_nodules", length = 255) private String skinNodules; - @Column(name = "skin_patch_discoloration", length = 50) + @Column(name = "skin_patch_discoloration", length = 255) private String skinPatchDiscoloration; - @Column(name = "recurrent_numbness", length = 10) + @Column(name = "recurrent_numbness", length = 255) private String recurrentNumbness; - @Column(name = "clawing_fingers", length = 10) + @Column(name = "clawing_fingers", length = 255) private String clawingFingers; - @Column(name = "tingling_numbness_extremities", length = 50) + @Column(name = "tingling_numbness_extremities", length = 255) private String tinglingNumbnessExtremities; - @Column(name = "inability_close_eyelid", length = 10) + @Column(name = "inability_close_eyelid", length = 255) private String inabilityCloseEyelid; - @Column(name = "difficulty_holding_objects", length = 50) + @Column(name = "difficulty_holding_objects", length = 255) private String difficultyHoldingObjects; - @Column(name = "weakness_feet", length = 10) + @Column(name = "weakness_feet", length = 255) private String weaknessFeet; @Column(name = "CreatedBy", length = 100) diff --git a/src/main/java/com/iemr/flw/service/MaternalHealthService.java b/src/main/java/com/iemr/flw/service/MaternalHealthService.java index 33de9bc9..e76d5a31 100644 --- a/src/main/java/com/iemr/flw/service/MaternalHealthService.java +++ b/src/main/java/com/iemr/flw/service/MaternalHealthService.java @@ -16,7 +16,7 @@ public interface MaternalHealthService { List getANCVisits(GetBenRequestHandler dto); - String saveANCVisit(List ancVisitDTOs); + String saveANCVisit(List ancVisitDTOs,Integer userId); List getPmsmaRecords(GetBenRequestHandler dto); 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 08bb264a..ab708dd8 100644 --- a/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java +++ b/src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java @@ -5,6 +5,7 @@ import com.iemr.flw.dto.identity.GetBenRequestHandler; import com.iemr.flw.dto.iemr.*; import com.iemr.flw.masterEnum.GroupName; +import com.iemr.flw.masterEnum.StateCode; import com.iemr.flw.repo.identity.BeneficiaryRepo; import com.iemr.flw.repo.iemr.*; import com.iemr.flw.service.IncentiveLogicService; @@ -26,6 +27,7 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoUnit; import java.util.*; import java.util.stream.Collectors; @@ -150,7 +152,7 @@ public List getANCVisits(GetBenRequestHandler dto) { } @Override - public String saveANCVisit(List ancVisitDTOs) { + public String saveANCVisit(List ancVisitDTOs,Integer userId) { try { List ancList = new ArrayList<>(); List ancCareList = new ArrayList<>(); @@ -206,7 +208,7 @@ public String saveANCVisit(List ancVisitDTOs) { ancVisitRepo.saveAll(ancList); ancCareRepo.saveAll(ancCareList); - checkAndAddIncentives(ancList); + checkAndAddIncentives(ancList,userId); logger.info("ANC visit details saved"); return "no of anc details saved: " + ancList.size(); } catch (Exception e) { @@ -620,112 +622,102 @@ record = new IncentiveActivityRecord(); } - private void checkAndAddIncentives(List ancList) { - - IncentiveActivity anc1Activity = - incentivesRepo.findIncentiveMasterByNameAndGroup("ANC_REGISTRATION_1ST_TRIM", GroupName.MATERNAL_HEALTH.getDisplayName()); - - IncentiveActivity ancFullActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FULL_ANC", GroupName.MATERNAL_HEALTH.getDisplayName()); - - IncentiveActivity ancFullActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("ANC_FOUR_CHECKUPS_SUPPORT", GroupName.ACTIVITY.getDisplayName()); - - IncentiveActivity comprehensiveAbortionActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("COMPREHENSIVE_ABORTION_CARE", GroupName.MATERNAL_HEALTH.getDisplayName()); - IncentiveActivity comprehensiveAbortionActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("COMPREHENSIVE_ABORTION_CARE", GroupName.ACTIVITY.getDisplayName()); - - IncentiveActivity identifiedHrpActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup("EPMSMA_HRP_IDENTIFIED", GroupName.MATERNAL_HEALTH.getDisplayName()); - IncentiveActivity identifiedHrpActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup("EPMSMA_HRP_IDENTIFIED", GroupName.ACTIVITY.getDisplayName()); + private void checkAndAddIncentives(List ancList, Integer userId) { + Integer stateId = userRepo.getUserRole(userId).get(0).getStateId(); + IncentiveActivity anc1Activity = null; + IncentiveActivity ancFullActivityAM = null; + IncentiveActivity identifiedHrpActivityAM = null; + IncentiveActivity comprehensiveAbortionActivityAM = null; + IncentiveActivity paiucdActivityAM = null; + + IncentiveActivity ancFullActivityCH = null; + IncentiveActivity comprehensiveAbortionActivityCH = null; + IncentiveActivity identifiedHrpActivityCH = null; + IncentiveActivity paiucdActivityCH = null; + + // ✅ State 5 — Assam + if (stateId.equals(StateCode.AM.getStateCode())) { + anc1Activity = incentivesRepo.findIncentiveMasterByNameAndGroup( + "ANC_REGISTRATION_1ST_TRIM", GroupName.MATERNAL_HEALTH.getDisplayName()); + ancFullActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FULL_ANC", GroupName.MATERNAL_HEALTH.getDisplayName()); + identifiedHrpActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "EPMSMA_HRP_IDENTIFIED", GroupName.MATERNAL_HEALTH.getDisplayName()); + comprehensiveAbortionActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "COMPREHENSIVE_ABORTION_CARE", GroupName.MATERNAL_HEALTH.getDisplayName()); + paiucdActivityAM = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_PAIUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + } - IncentiveActivity paiucdActivityAM = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PAIUCD", GroupName.FAMILY_PLANNING.getDisplayName()); + // ✅ State 8 + if (stateId.equals(StateCode.CG.getStateCode())) { + ancFullActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "ANC_FOUR_CHECKUPS_SUPPORT", GroupName.ACTIVITY.getDisplayName()); + comprehensiveAbortionActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "COMPREHENSIVE_ABORTION_CARE", GroupName.ACTIVITY.getDisplayName()); + identifiedHrpActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "EPMSMA_HRP_IDENTIFIED", GroupName.ACTIVITY.getDisplayName()); + paiucdActivityCH = incentivesRepo.findIncentiveMasterByNameAndGroup( + "FP_PAIUCD", GroupName.ACTIVITY.getDisplayName()); + } - IncentiveActivity paiucdActivityCH = - incentivesRepo.findIncentiveMasterByNameAndGroup("FP_PAIUCD", GroupName.ACTIVITY.getDisplayName()); + final IncentiveActivity finalAnc1Activity = anc1Activity; + final IncentiveActivity finalAncFullActivityAM = ancFullActivityAM; + final IncentiveActivity finalIdentifiedHrpActivityAM = identifiedHrpActivityAM; + final IncentiveActivity finalComprehensiveAbortionActivityAM = comprehensiveAbortionActivityAM; + final IncentiveActivity finalPaiucdActivityAM = paiucdActivityAM; + final IncentiveActivity finalAncFullActivityCH = ancFullActivityCH; + final IncentiveActivity finalComprehensiveAbortionActivityCH = comprehensiveAbortionActivityCH; + final IncentiveActivity finalIdentifiedHrpActivityCH = identifiedHrpActivityCH; + final IncentiveActivity finalPaiucdActivityCH = paiucdActivityCH; ancList.forEach(ancVisit -> { - if (paiucdActivityAM != null) { - if (ancVisit.getIsPaiucd()!= null) { - if (ancVisit.getIsPaiucd().equals("Yes")) { - recordAncRelatedIncentive(paiucdActivityAM, ancVisit); - - } - } + if (finalPaiucdActivityAM != null && ancVisit.getIsPaiucd() != null + && ancVisit.getIsPaiucd().equals("Yes")) { + recordAncRelatedIncentive(finalPaiucdActivityAM, ancVisit); } - if (paiucdActivityCH != null) { - - if (ancVisit.getIsPaiucd()!= null) { - if (ancVisit.getIsPaiucd().equals("Yes")) { - - recordAncRelatedIncentive(paiucdActivityCH, ancVisit); - - } - } - - + if (finalPaiucdActivityCH != null && ancVisit.getIsPaiucd() != null + && ancVisit.getIsPaiucd().equals("Yes")) { + recordAncRelatedIncentive(finalPaiucdActivityCH, ancVisit); } - if (anc1Activity != null) { - if (ancVisit.getAncVisit() != null) { - if (ancVisit.getAncVisit() == 1) { - recordAncRelatedIncentive(anc1Activity, ancVisit); - } - } + if (finalAnc1Activity != null && ancVisit.getAncVisit() != null + && ancVisit.getAncVisit() == 1) { + recordAncFirstTRIMIncentive(finalAnc1Activity, ancVisit); } - if (ancFullActivityAM != null) { - if (ancVisit.getAncVisit() == 4 || ancVisit.getAncVisit() == 2 || ancVisit.getAncVisit() == 3) { - recordAncRelatedIncentive(ancFullActivityAM, ancVisit); - } + if (finalAncFullActivityAM != null && ancVisit.getAncVisit() != null + && ancVisit.getAncVisit() == 4) { + recordFullAncIncentive(finalAncFullActivityAM, ancVisit); } - if (ancFullActivityCH != null) { - if (ancVisit.getAncVisit() != null) { - if (ancVisit.getAncVisit() == 4) { - recordAncRelatedIncentive(ancFullActivityCH, ancVisit); - } - } + if (finalAncFullActivityCH != null && ancVisit.getAncVisit() != null + && ancVisit.getAncVisit() == 4) { + recordAncRelatedIncentive(finalAncFullActivityCH, ancVisit); } - if (comprehensiveAbortionActivityAM != null) { - if (ancVisit.getIsAborted() != null) { - if (ancVisit.getIsAborted()) { - recordAncRelatedIncentive(comprehensiveAbortionActivityAM, ancVisit); - } - } + if (finalComprehensiveAbortionActivityAM != null && ancVisit.getIsAborted() != null + && ancVisit.getIsAborted()) { + recordAncRelatedIncentive(finalComprehensiveAbortionActivityAM, ancVisit); } - if (comprehensiveAbortionActivityCH != null) { - if (ancVisit.getIsAborted() != null) { - if (ancVisit.getIsAborted()) { - recordAncRelatedIncentive(comprehensiveAbortionActivityCH, ancVisit); - } - } - + if (finalComprehensiveAbortionActivityCH != null && ancVisit.getIsAborted() != null + && ancVisit.getIsAborted()) { + recordAncRelatedIncentive(finalComprehensiveAbortionActivityCH, ancVisit); } - if (identifiedHrpActivityAM != null) { - if (ancVisit.getIsHrpConfirmed() != null) { - if (ancVisit.getIsHrpConfirmed()) { - recordAncRelatedIncentive(identifiedHrpActivityAM, ancVisit); - } - } + if (finalIdentifiedHrpActivityAM != null && ancVisit.getIsHrpConfirmed() != null + && ancVisit.getIsHrpConfirmed()) { + recordAncRelatedIncentive(finalIdentifiedHrpActivityAM, ancVisit); } - if (identifiedHrpActivityCH != null) { - if (ancVisit.getIsHrpConfirmed() != null) { - if (ancVisit.getIsHrpConfirmed()) { - recordAncRelatedIncentive(identifiedHrpActivityCH, ancVisit); - } - } - + if (finalIdentifiedHrpActivityCH != null && ancVisit.getIsHrpConfirmed() != null + && ancVisit.getIsHrpConfirmed()) { + recordAncRelatedIncentive(finalIdentifiedHrpActivityCH, ancVisit); } - }); - - } private void recordAncRelatedIncentive(IncentiveActivity incentiveActivity, ANCVisit ancVisit) { @@ -748,6 +740,76 @@ record = new IncentiveActivityRecord(); } } + private void recordAncFirstTRIMIncentive(IncentiveActivity incentiveActivity, ANCVisit ancVisit) { + + if (ancVisit.getAncDate() == null || ancVisit.getLmpDate() == null) { + return; + } + + LocalDate ancLocalDate = ancVisit.getAncDate().toLocalDateTime().toLocalDate(); + LocalDate lmpLocalDate = ancVisit.getLmpDate().toLocalDateTime().toLocalDate(); + + long weeksBetween = ChronoUnit.WEEKS.between(lmpLocalDate, ancLocalDate); + + if (weeksBetween < 0 || weeksBetween >= 12) { + return; + } + + IncentiveActivityRecord record = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + ancVisit.getCreatedDate(), + ancVisit.getBenId() + ); + + Integer userId = userRepo.getUserIdByName(ancVisit.getCreatedBy()); + + if (record == null) { + record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(ancVisit.getLmpDate()); + record.setCreatedBy(ancVisit.getCreatedBy()); + record.setUpdatedDate(ancVisit.getLmpDate()); + record.setUpdatedBy(ancVisit.getCreatedBy()); + record.setStartDate(ancVisit.getLmpDate()); + record.setEndDate(ancVisit.getLmpDate()); + record.setBenId(ancVisit.getBenId()); + record.setAshaId(userId); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } + } + + private void recordFullAncIncentive(IncentiveActivity incentiveActivity, ANCVisit ancVisit) { + + IncentiveActivityRecord existRecord = recordRepo.findRecordByActivityIdCreatedDateBenId( + incentiveActivity.getId(), + ancVisit.getCreatedDate(), + ancVisit.getBenId() + ); + + + if (existRecord != null) { + return; + } + + Integer userId = userRepo.getUserIdByName(ancVisit.getCreatedBy()); + if (userId == null) { + return; + } + + IncentiveActivityRecord record = new IncentiveActivityRecord(); + record.setActivityId(incentiveActivity.getId()); + record.setCreatedDate(ancVisit.getAncDate()); + record.setCreatedBy(ancVisit.getCreatedBy()); + record.setUpdatedDate(ancVisit.getAncDate()); + record.setUpdatedBy(ancVisit.getCreatedBy()); + record.setStartDate(ancVisit.getAncDate()); + record.setEndDate(ancVisit.getAncDate()); + record.setBenId(ancVisit.getBenId()); + record.setAshaId(userId); + record.setAmount(Long.valueOf(incentiveActivity.getRate())); + recordRepo.save(record); + } public void sendAncDueTomorrowNotifications(String ashaId) {