diff --git a/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt b/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt index 7f64d9b8f5..525d1f5b74 100644 --- a/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt +++ b/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt @@ -453,8 +453,6 @@ fun List.toRuleDataValue( value = "" } RuleDataValue( - eventDate = event.eventDate()!!.toRuleEngineInstant(), - programStage = event.programStage()!!, dataElement = it.dataElement()!!, value = value!!, ) diff --git a/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RulesRepository.kt b/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RulesRepository.kt index 5b9653123c..2e521ec914 100644 --- a/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RulesRepository.kt +++ b/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RulesRepository.kt @@ -2,6 +2,7 @@ package org.dhis2.mobileProgramRules import android.os.Build import android.text.TextUtils.isEmpty +import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlinx.datetime.TimeZone import kotlinx.datetime.toLocalDateTime @@ -115,6 +116,9 @@ class RulesRepository(private val d2: D2) { .uid( event.organisationUnit(), ).blockingGet()?.code(), + createdDate = event.created() + ?.let { Instant.fromEpochMilliseconds(it.time) } + ?: Clock.System.now(), dataValues = event.trackedEntityDataValues()?.toRuleDataValue( event, d2.dataElementModule().dataElements(), @@ -202,6 +206,9 @@ class RulesRepository(private val d2: D2) { organisationUnitCode = d2.organisationUnitModule() .organisationUnits().uid(event.organisationUnit()) .blockingGet()?.code(), + createdDate = event.created() + ?.let { Instant.fromEpochMilliseconds(it.time) } + ?: Clock.System.now(), dataValues = event.trackedEntityDataValues()?.toRuleDataValue( event, @@ -319,6 +326,9 @@ class RulesRepository(private val d2: D2) { completedDate = event.completedDate()?.toRuleEngineLocalDate(), organisationUnit = event.organisationUnit()!!, organisationUnitCode = d2.organisationUnit(event.organisationUnit()!!)?.code(), + createdDate = event.created() + ?.let { Instant.fromEpochMilliseconds(it.time) } + ?: Clock.System.now(), dataValues = emptyList(), ) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9eebbb0159..0e2c124108 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,7 +9,7 @@ hilt = '2.47' jacoco = '0.8.10' designSystem = "0.4.1-20241212.083942-7" dhis2sdk = "1.11.1-SNAPSHOT" -ruleEngine = "3.0.0" +ruleEngine = "3.2.0" expressionParser = "1.1.0" appcompat = "1.6.1" annotation = "1.6.0" diff --git a/stock-usecase/src/main/java/org/dhis2/android/rtsm/services/rules/RuleValidationHelperImpl.kt b/stock-usecase/src/main/java/org/dhis2/android/rtsm/services/rules/RuleValidationHelperImpl.kt index 6b0f6c2b3b..7061515815 100644 --- a/stock-usecase/src/main/java/org/dhis2/android/rtsm/services/rules/RuleValidationHelperImpl.kt +++ b/stock-usecase/src/main/java/org/dhis2/android/rtsm/services/rules/RuleValidationHelperImpl.kt @@ -2,6 +2,8 @@ package org.dhis2.android.rtsm.services.rules import io.reactivex.Flowable import io.reactivex.Single +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant import org.apache.commons.lang3.math.NumberUtils import org.dhis2.android.rtsm.data.AppConfig import org.dhis2.android.rtsm.data.TransactionType @@ -58,9 +60,7 @@ class RuleValidationHelperImpl @Inject constructor( addAll( entryDataValues( entry.qty, - programStage.uid(), transaction, - entry.date, appConfig, ), ) @@ -73,8 +73,6 @@ class RuleValidationHelperImpl @Inject constructor( ruleEffect.data?.let { data -> dataValues.add( RuleDataValue( - entry.date.toRuleEngineInstant(), - programStage.uid(), ruleAction.field()!!, data, ), @@ -132,6 +130,7 @@ class RuleValidationHelperImpl @Inject constructor( programStage.name()!!, RuleEventStatus.ACTIVE, period.toRuleEngineInstant(), + period.toRuleEngineInstant(), period.toRuleEngineLocalDate(), period.toRuleEngineLocalDate(), organisationUnit, @@ -225,23 +224,28 @@ class RuleValidationHelperImpl @Inject constructor( .toFlowable().flatMapIterable { events -> events } .map { event -> RuleEvent( - event.uid(), - event.programStage()!!, + event = event.uid(), + programStage = event.programStage()!!, + programStageName = d2.programModule().programStages().uid(event.programStage()) .blockingGet()!!.name()!!, + status = if (event.status() == EventStatus.VISITED) { RuleEventStatus.ACTIVE } else { RuleEventStatus.valueOf(event.status()!!.name) }, - (event.eventDate() ?: Date()).toRuleEngineInstant(), - event.dueDate()?.toRuleEngineLocalDate(), - event.completedDate()?.toRuleEngineLocalDate(), - event.organisationUnit()!!, - d2.organisationUnitModule() + eventDate = (event.eventDate() ?: Date()).toRuleEngineInstant(), + createdDate = event.created() + ?.let { Instant.fromEpochMilliseconds(it.time) } + ?: Clock.System.now(), + dueDate = event.dueDate()?.toRuleEngineLocalDate(), + completedDate = event.completedDate()?.toRuleEngineLocalDate(), + organisationUnit = event.organisationUnit()!!, + organisationUnitCode = d2.organisationUnitModule() .organisationUnits().uid(event.organisationUnit()) .blockingGet()?.code(), - event.trackedEntityDataValues()?.toRuleDataValue( + dataValues = event.trackedEntityDataValues()?.toRuleDataValue( event, d2.dataElementModule().dataElements(), d2.programModule().programRuleVariables(), @@ -271,9 +275,7 @@ class RuleValidationHelperImpl @Inject constructor( private fun entryDataValues( qty: String?, - programStage: String, transaction: Transaction, - eventDate: Date, appConfig: AppConfig, ): List { val values = mutableListOf() @@ -284,8 +286,6 @@ class RuleValidationHelperImpl @Inject constructor( ConfigUtils.getTransactionDataElement(transaction.transactionType, appConfig) values.add( RuleDataValue( - eventDate = eventDate.toRuleEngineInstant(), - programStage = programStage, dataElement = deUid, value = qty, ), @@ -301,8 +301,6 @@ class RuleValidationHelperImpl @Inject constructor( ?.code()?.let { code -> values.add( RuleDataValue( - eventDate = eventDate.toRuleEngineInstant(), - programStage = programStage, dataElement = appConfig.distributedTo, value = code, ), diff --git a/stock-usecase/src/main/java/org/dhis2/android/rtsm/utils/DebugUtils.kt b/stock-usecase/src/main/java/org/dhis2/android/rtsm/utils/DebugUtils.kt index 02ef7fd640..7d248c38df 100644 --- a/stock-usecase/src/main/java/org/dhis2/android/rtsm/utils/DebugUtils.kt +++ b/stock-usecase/src/main/java/org/dhis2/android/rtsm/utils/DebugUtils.kt @@ -97,7 +97,7 @@ fun printRuleEffects( dataValues?.forEach { printRuleEngineData( buffer, - " DE = ${it.dataElement}, value = ${it.value}, eventDate= ${it.eventDate}", + " DE = ${it.dataElement}, value = ${it.value}", ) } printSeparator(buffer) diff --git a/stock-usecase/src/test/java/org/dhis2/android/rtsm/services/ProgramRuleTests.kt b/stock-usecase/src/test/java/org/dhis2/android/rtsm/services/ProgramRuleTests.kt index b7b8f7bc86..9d7338d3a8 100644 --- a/stock-usecase/src/test/java/org/dhis2/android/rtsm/services/ProgramRuleTests.kt +++ b/stock-usecase/src/test/java/org/dhis2/android/rtsm/services/ProgramRuleTests.kt @@ -138,6 +138,7 @@ class ProgramRuleTests { "", RuleEventStatus.ACTIVE, Date().toRuleEngineInstant(), + Date().toRuleEngineInstant(), Date().toRuleEngineLocalDate(), null, "", @@ -145,36 +146,26 @@ class ProgramRuleTests { listOf( // PRevious Stock Balance RuleDataValue( - Date().toRuleEngineInstant(), - "", "oc8tn8CewiP", "3", ), // PSM Stock received RuleDataValue( - Date().toRuleEngineInstant(), - "", "j3ydinp6Qp8", "4", ), // PSM- Stock consumed distributed RuleDataValue( - Date().toRuleEngineInstant(), - "", "lpGYJoVUudr", "2", ), // PSM- Stock discarded RuleDataValue( - Date().toRuleEngineInstant(), - "", "I7cmT3iXT0y", "1", ), // PSM- Stock corrected RuleDataValue( - Date().toRuleEngineInstant(), - "", "ej1YwWaYGmm", "3", ),