← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22306: tracker-capture: rules engine is now applied during relative regsitration

 

------------------------------------------------------------
revno: 22306
committer: Abyot Asalefew Gizaw <abyot@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2016-03-15 09:14:44 +0100
message:
  tracker-capture: rules engine is now applied during relative regsitration
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/registration.html
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js


--
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-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2016-03-14 14:35:45 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2016-03-15 08:14:44 +0000
@@ -29,12 +29,10 @@
     $scope.trackedEntityForm = null;
     $scope.customForm = null;    
     $scope.selectedTei = {};
-    $scope.tei = {};
+    $scope.tei = {};    
+    $scope.hiddenFields = {};    
+    $scope.helpTexts = {};
     $scope.registrationMode = 'REGISTRATION';    
-    $scope.hiddenFields = {};
-    
-    $scope.helpTexts = {};
-    
 
     $scope.attributesById = CurrentSelection.getAttributesById();
     if(!$scope.attributesById){
@@ -252,7 +250,7 @@
         if(!$scope.selectedTei.trackedEntityInstance){
             $scope.selectedTei.trackedEntity = $scope.tei.trackedEntity = $scope.selectedProgram && $scope.selectedProgram.trackedEntity && $scope.selectedProgram.trackedEntity.id ? $scope.selectedProgram.trackedEntity.id : $scope.trackedEntities.selected.id;
             $scope.selectedTei.orgUnit = $scope.tei.orgUnit = $scope.selectedOrgUnit.id;
-            $scope.selectedTei.attributes = $scope.selectedTei.attributes = [];
+            $scope.selectedTei.attributes = $scope.tei.attributes = [];
         }
         
         //get tei attributes and their values

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/registration.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/registration.html	2016-02-15 10:16:24 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/registration.html	2016-03-15 08:14:44 +0000
@@ -48,7 +48,7 @@
             <div class="alert alert-warning">{{'form_is_empty_fill_at_least_one'| translate}}</div> 
         </div>
 
-        <button type="button" class="btn btn-primary small-horizonal-spacing" ng-click="registerEntity()">{{'save'| translate}}</button>        
+        <button type="button" class="btn btn-primary" ng-click="registerEntity()">{{'save'| translate}}</button>        
         <button type="button" class="btn btn-default small-horizonal-spacing" ng-click="showRegistration()">{{'cancel'| translate}}</button>
 
     </form>

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js	2016-03-14 10:36:27 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js	2016-03-15 08:14:44 +0000
@@ -512,12 +512,23 @@
                 DialogService,
                 CurrentSelection,
                 DateUtils,
+                EventUtils,
+                DHIS2EventFactory,
                 RegistrationService,
-                SessionStorageService) {
+                SessionStorageService,
+                TrackerRulesExecutionService,
+                TEIGridService) {
     $scope.selectedOrgUnit = SessionStorageService.get('SELECTED_OU');
     $scope.enrollment = {enrollmentDate: '', incidentDate: ''};    
     $scope.attributesById = CurrentSelection.getAttributesById();
     $scope.maxOptionSize = 30;
+    $scope.today = DateUtils.getToday();
+    $scope.trackedEntityForm = null;
+    $scope.customForm = null;
+    $scope.selectedTei = {};
+    $scope.tei = {};
+    $scope.hiddenFields = {};
+    $scope.editingDisabled = false;
     
     var selections = CurrentSelection.get();
     $scope.programs = selections.prs;
@@ -547,8 +558,8 @@
     }
     
     var assignInheritance = function(){        
+        $scope.selectedTei = {};
         if($scope.addingRelationship){
-            $scope.selectedTei = {};
             var t = angular.copy( CurrentSelection.getRelationshipOwner() );
             angular.forEach(t.attributes, function(att){
                 t[att.attribute] = att.value;
@@ -561,9 +572,6 @@
             });
             t = {};
         }
-        else{
-            $scope.selectedTei = {};
-        }
     };
     
     var getRules = function(){
@@ -579,7 +587,7 @@
     if(angular.isObject($scope.programs) && $scope.programs.length === 1){
         $scope.selectedProgramForRelative = $scope.programs[0];
         AttributesFactory.getByProgram($scope.selectedProgramForRelative).then(function(atts){
-            $scope.attributes = atts;            
+            $scope.attributes = TEIGridService.generateGridColumns(atts, null,false).columns;
             assignInheritance();
             getRules();
         });
@@ -589,10 +597,9 @@
     $scope.$watch('selectedProgramForRelative', function() {        
         $scope.trackedEntityForm = null;
         $scope.customForm = null;
-        $scope.customFormExists = false;
-        
+        $scope.customFormExists = false;        
         AttributesFactory.getByProgram($scope.selectedProgramForRelative).then(function(atts){
-            $scope.attributes = atts;                        
+            $scope.attributes = TEIGridService.generateGridColumns(atts, null,false).columns;                       
             if($scope.selectedProgramForRelative && $scope.selectedProgramForRelative.id && $scope.selectedProgramForRelative.dataEntryForm && $scope.selectedProgramForRelative.dataEntryForm.htmlCode){
                 $scope.customFormExists = true;
                 $scope.trackedEntityForm = $scope.selectedProgramForRelative.dataEntryForm;  
@@ -602,7 +609,6 @@
                 $scope.trackedEntityForm.displayIncidentDate = $scope.selectedProgramForRelative.displayIncidentDate;
                 $scope.customForm = CustomFormService.getForTrackedEntity($scope.trackedEntityForm, 'RELATIONSHIP');
             }
-
             assignInheritance();
             getRules();                
         });
@@ -633,7 +639,10 @@
         //get tei attributes and their values
         //but there could be a case where attributes are non-mandatory and
         //registration form comes empty, in this case enforce at least one value
-        $scope.tei = {trackedEntity: selectedTrackedEntity, orgUnit: $scope.selectedOrgUnit.id};
+        $scope.selectedTei.trackedEntity = $scope.tei.trackedEntity = selectedTrackedEntity; 
+        $scope.selectedTei.orgUnit = $scope.tei.orgUnit = $scope.selectedOrgUnit.id;
+        $scope.selectedTei.attributes = $scope.tei.attributes = [];
+        
         var result = RegistrationService.processForm($scope.tei, $scope.selectedTei, $scope.attributesById);
         $scope.formEmpty = result.formEmpty;
         $scope.tei = result.tei;
@@ -651,15 +660,22 @@
                 if($scope.selectedProgramForRelative){    
                     //enroll TEI
                     var enrollment = {};
-                        enrollment.trackedEntityInstance = $scope.tei.trackedEntityInstance;
-                        enrollment.program = $scope.selectedProgramForRelative.id;
-                        enrollment.status = 'ACTIVE';
-                        enrollment.orgUnit = $scope.selectedOrgUnit.id;
-                        enrollment.enrollmentDate = $scope.selectedEnrollment.enrollmentDate;
-                        enrollment.incidentDate = $scope.selectedEnrollment.incidentDate === '' ? $scope.selectedEnrollment.enrollmentDate : $scope.selectedEnrollment.incidentDate;
+                    enrollment.trackedEntityInstance = $scope.tei.trackedEntityInstance;
+                    enrollment.program = $scope.selectedProgramForRelative.id;
+                    enrollment.status = 'ACTIVE';
+                    enrollment.orgUnit = $scope.selectedOrgUnit.id;
+                    enrollment.enrollmentDate = $scope.selectedEnrollment.enrollmentDate;
+                    enrollment.incidentDate = $scope.selectedEnrollment.incidentDate === '' ? $scope.selectedEnrollment.enrollmentDate : $scope.selectedEnrollment.incidentDate;
                     EnrollmentService.enroll(enrollment).then(function(enrollmentResponse){
                         var en = enrollmentResponse.response && enrollmentResponse.response.importSummaries && enrollmentResponse.response.importSummaries[0] ? enrollmentResponse.response.importSummaries[0] : {};
-                        if(en.reference && en.status === 'SUCCESS'){                            
+                        if(en.reference && en.status === 'SUCCESS'){
+                            enrollment.enrollment = en.reference;
+                            $scope.selectedEnrollment = enrollment;
+                            var dhis2Events = EventUtils.autoGenerateEvents($scope.tei.trackedEntityInstance, $scope.selectedProgramForRelative, $scope.selectedOrgUnit, enrollment);
+                            if(dhis2Events.events.length > 0){
+                                DHIS2EventFactory.create(dhis2Events).then(function(){                                    
+                                });
+                            }
                         }
                         else{
                             //enrollment has failed
@@ -710,6 +726,49 @@
         }        
     };
     
+    $scope.executeRules = function () {
+        var flag = {debug: true, verbose: false};
+        
+        //repopulate attributes with updated values
+        $scope.selectedTei.attributes = [];        
+        angular.forEach($scope.attributes, function(metaAttribute){
+            var newAttributeInArray = {attribute:metaAttribute.id,
+                code:metaAttribute.code,
+                displayName:metaAttribute.displayName,
+                type:metaAttribute.valueType
+            };
+            if($scope.selectedTei[newAttributeInArray.attribute]){
+                newAttributeInArray.value = $scope.selectedTei[newAttributeInArray.attribute];
+            }
+            
+           $scope.selectedTei.attributes.push(newAttributeInArray);
+        });
+        
+        if($scope.selectedProgram && $scope.selectedProgram.id){
+            TrackerRulesExecutionService.executeRules($scope.allProgramRules, 'registration', null, null, $scope.selectedTei, $scope.selectedEnrollment, flag);
+        }        
+    };
+    
+    //check if field is hidden
+    $scope.isHidden = function (id) {
+        //In case the field contains a value, we cant hide it. 
+        //If we hid a field with a value, it would falsely seem the user was aware that the value was entered in the UI.        
+        return $scope.selectedTei[id] ? false : $scope.hiddenFields[id];
+    };
+    
+    $scope.teiValueUpdated = function(tei, field){
+        $scope.executeRules();
+    };
+    
+    //listen for rule effect changes
+    $scope.$on('ruleeffectsupdated', function(){
+        $scope.warningMessages = [];
+        var effectResult = TrackerRulesExecutionService.processRuleEffectAttribute('registration', $scope.selectedTei, $scope.tei, $scope.attributesById, $scope.hiddenFields, $scope.warningMessages);
+        $scope.selectedTei = effectResult.selectedTei;
+        $scope.hiddenFields = effectResult.hiddenFields;
+        $scope.warningMessages = effectResult.warningMessages;
+    });
+    
     $scope.interacted = function(field) {
         var status = false;
         if(field){