1- function ReportScheduleOptions ( formErrors , success ) {
1+ function ReportScheduleOptions ( orgUnits , formErrors , success ) {
22 var periodId = "#periodType" , startDtId = "#startDate" , displayPeriodId = "#reportingPeriod" ;
33 var dtCh = "/" , minYear = 1900 , maxYear = 2100 ;
44 var applicableOrgUnits = [ ] ;
@@ -11,17 +11,6 @@ function ReportScheduleOptions(formErrors,success) {
1111 self . validateInput ( $ ( this ) . val ( ) ) ;
1212 } ) ;
1313
14- var checkboxes = $ ( "input[type='checkbox']" ) ;
15- checkboxes . click ( function ( ) {
16- if ( checkboxes . is ( ":checked" ) ) {
17- isChecked = true ;
18- }
19- else {
20- isChecked = false ;
21- }
22- self . disableSubmitAndPreview ( ) ;
23- } ) ;
24-
2514 $ ( '#scheduleStartDate' ) . datepicker ( {
2615 autoclose : true ,
2716 onRender : function ( date ) {
@@ -115,24 +104,6 @@ function ReportScheduleOptions(formErrors,success) {
115104 } ) ;
116105 } ) ;
117106
118- $ ( "input[name=selectedFacilities]" ) . bind ( "click" , function ( e ) {
119- clearErrors ( ) ;
120- clearSuccess ( ) ;
121- var orgUnitId = $ ( e . target ) . attr ( "data-orgunit" ) ;
122- var arrayLength = self . applicableOrgUnits . length ;
123- var found = false ;
124- for ( var i = 0 ; i < arrayLength ; i ++ ) {
125- var orgUnit = self . applicableOrgUnits [ i ] ;
126- if ( orgUnit . id === orgUnitId ) {
127- found = true ;
128- }
129- }
130- if ( ! found ) {
131- showErrors ( "The selected organization is not applicable for this report." ) ;
132- e . preventDefault ( ) ;
133- }
134- } ) ;
135-
136107 $ ( "input[name=scheduleType]" ) . bind ( "click" , function ( e ) {
137108 var val = $ ( 'input[type=radio]:checked' ) . val ( ) ;
138109 if ( val == "repeat" ) {
@@ -332,6 +303,7 @@ function ReportScheduleOptions(formErrors,success) {
332303 if ( results ) {
333304 if ( results . hasOwnProperty ( 'organisationUnits' ) ) {
334305 self . applicableOrgUnits = results . organisationUnits ;
306+ showApplicableOrgUnits ( orgUnits ) ;
335307 }
336308 }
337309 } ) . fail ( function ( ) {
@@ -340,6 +312,39 @@ function ReportScheduleOptions(formErrors,success) {
340312 } ) ;
341313 } ;
342314
315+ var showApplicableOrgUnits = function ( availableOrgUnits ) {
316+ var availableApplicableOrgUnits = getAvailableApplicableOrgUnits ( availableOrgUnits , self . applicableOrgUnits ) ;
317+ if ( availableApplicableOrgUnits . length == 0 ) {
318+ showErrors ( "This report is not applicable to any of the available facilities." ) ;
319+ $ ( '#createReportSchedule' ) . attr ( 'hidden' , true ) ;
320+ }
321+ else {
322+ var template = $ ( '#template_applicable_org_units' ) . html ( ) ;
323+ Mustache . parse ( template ) ;
324+ var rendered = Mustache . render ( template , availableApplicableOrgUnits ) ;
325+ $ ( '#applicableOrgUnits' ) . html ( rendered ) ;
326+ $ ( '#applicableOrgUnits' ) . attr ( 'hidden' , false ) ;
327+ var checkboxes = $ ( "input[type='checkbox']" ) ;
328+ checkboxes . click ( function ( ) {
329+ if ( checkboxes . is ( ":checked" ) ) {
330+ isChecked = true ;
331+ }
332+ else {
333+ isChecked = false ;
334+ }
335+ self . disableSubmitAndPreview ( ) ;
336+ } ) ;
337+ }
338+ }
339+
340+ var getAvailableApplicableOrgUnits = function ( availableOrgUnits , applicableOrgUnits ) {
341+ var filteredOrgUnits = availableOrgUnits . filter ( function ( availableUnit ) {
342+ return applicableOrgUnits . find ( function ( applicableUnit ) {
343+ return applicableUnit . id == availableUnit . orgUnitId } )
344+ } ) ;
345+ return filteredOrgUnits ;
346+ }
347+
343348 var getDhisNames = function ( response ) {
344349 var dataElements = { } ;
345350 var comboOptions = { } ;
@@ -397,7 +402,7 @@ function ReportScheduleOptions(formErrors,success) {
397402
398403 fetchOrgUnits ( ) ;
399404 showErrors ( formErrors ) ;
400- showSuccess ( success )
405+ showSuccess ( success ) ;
401406}
402407
403408var toggleFacilityDetails = function ( facilityId , show ) {
0 commit comments