← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 10148: Fix bug Program Statistics

 

------------------------------------------------------------
revno: 10148
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-03-12 10:33:20 +0700
message:
  Fix bug Program Statistics 
modified:
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java
  dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-03-11 16:36:56 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramStageInstanceService.java	2013-03-12 03:33:20 +0000
@@ -363,10 +363,15 @@
 
         // Average number of stages for complete programs
 
-        int stageCompleted = averageNumberCompletedProgramInstance( program, orgunitIds, startDate, endDate, true );
         grid.addRow();
         grid.addValue( i18n.getString( "average_number_of_stages_for_complete_programs" ) );
-        grid.addValue( stageCompleted );
+        double percent = 0.0;
+        if ( totalCompleted != 0 )
+        {
+            int stageCompleted = averageNumberCompletedProgramInstance( program, orgunitIds, startDate, endDate, true );
+            percent = (stageCompleted + 0.0) / totalCompleted;
+        }
+        grid.addValue( format.formatValue( percent ) );
         grid.addValue( "" );
         grid.addValue( "" );
         grid.addValue( "" );
@@ -432,7 +437,7 @@
 
             // Visits done (%)
 
-            double percent = 0.0;
+            percent = 0.0;
             if ( totalAll != 0 )
             {
                 percent = (totalVisit + 0.0) * 100 / totalAll;

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java	2013-03-11 16:07:20 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramInstanceStore.java	2013-03-12 03:33:20 +0000
@@ -134,13 +134,10 @@
     public Collection<ProgramInstance> get( Program program, Collection<Integer> orgunitIds, Date startDate,
         Date endDate, int min, int max )
     {
-        return getCriteria( Restrictions.eq( "program", program ), 
-            Restrictions.ge( "enrollmentDate", startDate ), 
-            Restrictions.le( "enrollmentDate", endDate ) )
-            .createAlias( "patient", "patient" )
+        return getCriteria( Restrictions.eq( "program", program ), Restrictions.ge( "enrollmentDate", startDate ),
+            Restrictions.le( "enrollmentDate", endDate ) ).createAlias( "patient", "patient" )
             .createAlias( "patient.organisationUnit", "organisationUnit" )
-            .add( Restrictions.in( "organisationUnit.id", orgunitIds ) )
-            .addOrder( Order.asc( "patient.id" ) )
+            .add( Restrictions.in( "organisationUnit.id", orgunitIds ) ).addOrder( Order.asc( "patient.id" ) )
             .setFirstResult( min ).setMaxResults( max ).list();
     }
 
@@ -155,12 +152,9 @@
     public int count( Program program, Collection<Integer> orgunitIds, Date startDate, Date endDate )
     {
         Number rs = (Number) getCriteria( Restrictions.eq( "program", program ),
-            Restrictions.ge( "enrollmentDate", startDate ), 
-            Restrictions.le( "enrollmentDate", endDate ) )
-            .createAlias( "patient", "patient" )
-            .createAlias( "patient.organisationUnit", "organisationUnit" )
-            .add( Restrictions.in( "organisationUnit.id", orgunitIds ) )
-            .setProjection( Projections.rowCount() )
+            Restrictions.ge( "enrollmentDate", startDate ), Restrictions.le( "enrollmentDate", endDate ) )
+            .createAlias( "patient", "patient" ).createAlias( "patient.organisationUnit", "organisationUnit" )
+            .add( Restrictions.in( "organisationUnit.id", orgunitIds ) ).setProjection( Projections.rowCount() )
             .uniqueResult();
 
         return rs != null ? rs.intValue() : 0;
@@ -169,8 +163,7 @@
     public int count( Program program, Collection<Integer> orgunitIds, Date startDate, Date endDate, boolean completed )
     {
         Criteria criteria = getCriteria( Restrictions.eq( "program", program ) );
-        criteria.createAlias( "patient", "patient" )
-            .createAlias( "patient.organisationUnit", "organisationUnit" )
+        criteria.createAlias( "patient", "patient" ).createAlias( "patient.organisationUnit", "organisationUnit" )
             .add( Restrictions.in( "organisationUnit.id", orgunitIds ) )
             .add( Restrictions.eq( "completed", completed ) );
         if ( completed )
@@ -194,8 +187,7 @@
             Restrictions.le( "enrollmentDate", endDate ) ).createAlias( "patient", "patient" )
             .createAlias( "programStageInstances", "programStageInstance" )
             .createAlias( "patient.organisationUnit", "organisationUnit" )
-            .add( Restrictions.in( "organisationUnit.id", orgunitIds ) )
-            .add( Restrictions.eq( "completed", true ) )
+            .add( Restrictions.in( "organisationUnit.id", orgunitIds ) ).add( Restrictions.eq( "completed", true ) )
             .add( Restrictions.eq( "programStageInstance.completed", false ) ).list();
     }
 
@@ -207,8 +199,7 @@
             .createAlias( "patient.organisationUnit", "organisationUnit" )
             .add( Restrictions.in( "organisationUnit.id", orgunitIds ) ).add( Restrictions.eq( "completed", true ) )
             .add( Restrictions.eq( "programStageInstance.completed", false ) )
-            .setProjection( Projections.projectionList().add(Projections.countDistinct("id") ) )
-            .uniqueResult();
+            .setProjection( Projections.projectionList().add( Projections.countDistinct( "id" ) ) ).uniqueResult();
 
         return rs != null ? rs.intValue() : 0;
     }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-03-11 16:07:20 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/hibernate/HibernateProgramStageInstanceStore.java	2013-03-12 03:33:20 +0000
@@ -66,6 +66,7 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.program.Program;
 import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
 import org.hisp.dhis.program.ProgramStage;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceStore;
@@ -140,6 +141,13 @@
         this.patientService = patientService;
     }
 
+    private ProgramInstanceService programInstanceService;
+
+    public void setProgramInstanceService( ProgramInstanceService programInstanceService )
+    {
+        this.programInstanceService = programInstanceService;
+    }
+
     // -------------------------------------------------------------------------
     // Implemented methods
     // -------------------------------------------------------------------------
@@ -723,6 +731,7 @@
         criteria.createAlias( "patient.organisationUnit", "regOrgunit" );
         criteria.add( Restrictions.eq( "programStage", programStage ) );
         criteria.add( Restrictions.isNull( "programInstance.endDate" ) );
+        criteria.add( Restrictions.isNull( "executionDate" ) );
         criteria.add( Restrictions.between( "dueDate", startDate, now ) );
         criteria.add( Restrictions.in( "regOrgunit.id", orgunitIds ) );
         criteria.setProjection( Projections.rowCount() ).uniqueResult();
@@ -806,22 +815,18 @@
         {
             criteria.createAlias( "programInstance.patient", "patient" );
             criteria.createAlias( "patient.organisationUnit", "regOrgunit" );
-            criteria.add( Restrictions.or(
-                Restrictions.and( Restrictions.eq( "completed", true ),
-                    Restrictions.between( "executionDate", startDate, endDate ),
-                    Restrictions.in( "organisationUnit.id", orgunitIds ) ),
-                Restrictions.and( Restrictions.eq( "completed", false ),
-                        Restrictions.isNotNull( "executionDate" ),
-                        Restrictions.between( "executionDate", startDate, endDate ),
-                        Restrictions.in( "organisationUnit.id", orgunitIds ) ),
-                Restrictions.and( Restrictions.eq( "completed", false ),
-                        Restrictions.isNull( "executionDate" ),
-                        Restrictions.between( "dueDate", startDate, endDate ),
-                        Restrictions.in( "regOrgunit.id", orgunitIds ) ),
-                Restrictions.and( Restrictions.eq( "status", ProgramStageInstance.SKIPPED_STATUS ),
-                         Restrictions.between( "dueDate", startDate, endDate ),
-                        Restrictions.in( "regOrgunit.id", orgunitIds ) )
-                ) );
+            criteria.add( Restrictions.or( Restrictions.and( Restrictions.eq( "completed", true ),
+                Restrictions.between( "executionDate", startDate, endDate ),
+                Restrictions.in( "organisationUnit.id", orgunitIds ) ), Restrictions.and(
+                Restrictions.eq( "completed", false ), Restrictions.isNotNull( "executionDate" ),
+                Restrictions.between( "executionDate", startDate, endDate ),
+                Restrictions.in( "organisationUnit.id", orgunitIds ) ),
+                Restrictions.and( Restrictions.eq( "completed", false ), Restrictions.isNull( "executionDate" ),
+                    Restrictions.between( "dueDate", startDate, endDate ),
+                    Restrictions.in( "regOrgunit.id", orgunitIds ) ), Restrictions.and(
+                    Restrictions.eq( "status", ProgramStageInstance.SKIPPED_STATUS ),
+                    Restrictions.between( "dueDate", startDate, endDate ),
+                    Restrictions.in( "regOrgunit.id", orgunitIds ) ) ) );
         }
         else
         {
@@ -1897,6 +1902,8 @@
     public int averageNumberCompleted( Program program, Collection<Integer> orgunitIds, Date startDate, Date endDate,
         Boolean completed )
     {
+        Collection<ProgramInstance> programInstances = programInstanceService.getUnenrollment( program, orgunitIds,
+            startDate, endDate );
         Criteria criteria = getCriteria();
         criteria.createAlias( "programInstance", "programInstance" );
         criteria.createAlias( "programStage", "programStage" );
@@ -1904,8 +1911,12 @@
         criteria.add( Restrictions.eq( "programInstance.program", program ) );
         criteria.add( Restrictions.eq( "programInstance.completed", completed ) );
         criteria.add( Restrictions.in( "organisationUnit.id", orgunitIds ) );
-        criteria.add( Restrictions.between( "executionDate", startDate, endDate ) );
+        criteria.add( Restrictions.between( "programInstance.endDate", startDate, endDate ) );
         criteria.add( Restrictions.eq( "completed", true ) );
+        if ( programInstances != null && programInstances.size() > 0 )
+        {
+            criteria.add( Restrictions.not( Restrictions.in( "programInstance", programInstances ) ) );
+        }
 
         Number rs = (Number) criteria.setProjection( Projections.rowCount() ).uniqueResult();
         return rs != null ? rs.intValue() : 0;

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2013-03-09 15:25:42 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/resources/META-INF/dhis/beans.xml	2013-03-12 03:33:20 +0000
@@ -37,6 +37,8 @@
 		<property name="patientAuditService" ref="org.hisp.dhis.patient.PatientAuditService" />
 		<property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
 		<property name="patientService" ref="org.hisp.dhis.patient.PatientService" />
+		<property name="programInstanceService"
+			ref="org.hisp.dhis.program.ProgramInstanceService" />
 	</bean>
 
 	<bean id="org.hisp.dhis.program.ProgramInstanceStore"