Skip to content

Commit

Permalink
Merge pull request #110 from motech-implementations/cumulative-benefi…
Browse files Browse the repository at this point in the history
…ciary-fix

Cumulative beneficiary fix
  • Loading branch information
Mandar-Beehyv authored Nov 25, 2024
2 parents b326876 + 052c358 commit a638821
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,7 @@ public ResponseEntity<Long> getCumulativeBeneficiaries(){


Long cumulativeJoinedSubscription = kilkariCallReportDao.getCumulativeJoinedSubscription(locationId, locationType, toDate);
LOGGER.info("cumulativecount:{},date:{},locationType:{},locationId:{}",cumulativeJoinedSubscription,toDate,locationType,locationId);
return ResponseEntity.ok(cumulativeJoinedSubscription);
// Long ashaStarted = 0L;
// Long ashaCompleted = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Integer dis
"COUNT(Case when s.subscription_status = 'COMPLETED' THEN s.subscription_id END) AS subscriptions_completed, " +
"0 AS subscriptions_rejected , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) >= 169 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
"FROM Beneficiary b " +
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Integer sta
"COUNT(CASE WHEN s.subscription_status = 'COMPLETED' THEN s.subscription_id END) AS subscriptions_completed , " +
"0 AS subscriptions_rejected , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) >= 169 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
"FROM Beneficiary b " +
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Date fromDa
"COUNT(CASE WHEN s.subscription_status = 'COMPLETED' THEN s.subscription_id END) AS subscriptions_completed ," +
"0 AS subscriptions_rejected , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child, "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) >= 169 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child, "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
"FROM Beneficiary b " +
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<KilkariSubscriberRegistrationDateDto> allCountOffReports(Integer blo
"COUNT(CASE WHEN s.subscription_status = 'COMPLETED' THEN s.subscription_id END) AS subscriptions_completed , " +
"0 AS subscriptions_rejected , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) BETWEEN -90 AND 168 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_PW , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) BETWEEN 169 AND 504 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 2 OR DATEDIFF(registrationDate, s.start_date) >= 169 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Received_for_Child , "+
"COUNT(DISTINCT CASE WHEN s.subscriptionPack_id = 1 AND DATEDIFF(registrationDate, s.start_date) < -90 THEN s.subscription_id ELSE NULL END) AS Subscriptions_Ineligible "+
"FROM Beneficiary b " +
" INNER JOIN subscriptions s ON b.id = s.beneficiary_id AND s.subscription_id = (SELECT MAX(subscription_id) FROM subscriptions WHERE beneficiary_id = b.id) "+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.beehyv.nmsreporting.dao.KilkariCallReportDao;
import com.beehyv.nmsreporting.model.KilkariCalls;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -51,20 +51,29 @@ public KilkariCalls getKilkariCallreport(Integer locationId, String locationTyp

@Override
public Long getCumulativeJoinedSubscription(Long locationId, String locationType, Date toDate) {
Criteria criteria = createEntityCriteria();
String hql;

if (locationId == 0 && "State".equalsIgnoreCase(locationType)) {
criteria.add(Restrictions.eq("locationType", locationType));
String sql = "SELECT COALESCE(SUM(CAST(total_beneficiaries AS SIGNED)), 0) " +
"FROM agg_kilkari_call_report " +
"WHERE location_type = :locationType AND date = (SELECT date FROM agg_kilkari_call_report ORDER BY date DESC LIMIT 1)";
Query query = getSession().createSQLQuery(sql);
query.setParameter("locationType", locationType);
// query.setParameter("toDate", toDate);
Long result = ((Number) query.uniqueResult()).longValue();
return result != null ? result : 0L;
} else {
criteria.add(Restrictions.eq("locationId", locationId));
criteria.add(Restrictions.eq("locationType", locationType));
String sql = "SELECT COALESCE(SUM(CAST(total_beneficiaries AS SIGNED)), 0) " +
"FROM agg_kilkari_call_report " +
"WHERE location_id = :locationId AND location_type = :locationType AND date = (SELECT date FROM agg_kilkari_call_report ORDER BY date DESC LIMIT 1)";
Query query = getSession().createSQLQuery(sql);
query.setParameter("locationId", locationId);
query.setParameter("locationType", locationType);
// query.setParameter("toDate", toDate);
Long result = ((Number) query.uniqueResult()).longValue();
return result != null ? result : 0L;
}
criteria.add(Restrictions.eq("date", toDate));

criteria.setProjection(Projections.sum("uniqueBeneficiaries"));

Long result = (Long) criteria.uniqueResult();
return result != null ? result : 0L;
}
}

0 comments on commit a638821

Please sign in to comment.