Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ANCVisitDTO> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface MaternalHealthService {

List<ANCVisitDTO> getANCVisits(GetBenRequestHandler dto);

String saveANCVisit(List<ANCVisitDTO> ancVisitDTOs);
String saveANCVisit(List<ANCVisitDTO> ancVisitDTOs,Integer userId);

List<PmsmaDTO> getPmsmaRecords(GetBenRequestHandler dto);

Expand Down
233 changes: 148 additions & 85 deletions src/main/java/com/iemr/flw/service/impl/MaternalHealthServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -144,7 +145,7 @@ public List<ANCVisitDTO> getANCVisits(GetBenRequestHandler dto) {
}

@Override
public String saveANCVisit(List<ANCVisitDTO> ancVisitDTOs) {
public String saveANCVisit(List<ANCVisitDTO> ancVisitDTOs,Integer userId) {
try {
List<ANCVisit> ancList = new ArrayList<>();
List<AncCare> ancCareList = new ArrayList<>();
Expand Down Expand Up @@ -200,7 +201,7 @@ public String saveANCVisit(List<ANCVisitDTO> 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) {
Expand Down Expand Up @@ -613,112 +614,104 @@ record = new IncentiveActivityRecord();
}


private void checkAndAddIncentives(List<ANCVisit> 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<ANCVisit> 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(5)) {
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(8)) {
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) {
Expand All @@ -741,6 +734,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) {
Expand Down