From 680abfdb3596889108306fcf8c22ea271d5fab47 Mon Sep 17 00:00:00 2001 From: Pablo Pajuelo Cabezas Date: Wed, 4 Dec 2024 10:12:05 +0100 Subject: [PATCH] periods not being generated properly --- .../org/dhis2/form/data/EventRepository.kt | 93 +++++++++++-------- 1 file changed, 52 insertions(+), 41 deletions(-) diff --git a/form/src/main/java/org/dhis2/form/data/EventRepository.kt b/form/src/main/java/org/dhis2/form/data/EventRepository.kt index 05bee4fb1a..57164c7278 100644 --- a/form/src/main/java/org/dhis2/form/data/EventRepository.kt +++ b/form/src/main/java/org/dhis2/form/data/EventRepository.kt @@ -38,7 +38,6 @@ import org.hisp.dhis.android.core.event.EventStatus import org.hisp.dhis.android.core.imports.ImportStatus import org.hisp.dhis.android.core.period.PeriodType import org.hisp.dhis.android.core.program.Program -import org.hisp.dhis.android.core.program.ProgramStage import org.hisp.dhis.android.core.program.ProgramStageDataElement import org.hisp.dhis.android.core.program.ProgramStageSection import org.hisp.dhis.android.core.program.SectionRenderingType @@ -189,7 +188,8 @@ class EventRepository( } override fun validationStrategy(): ValidationStrategy? { - return d2.programModule().programStages().uid(programStage?.uid()).blockingGet()?.validationStrategy() + return d2.programModule().programStages().uid(programStage?.uid()).blockingGet() + ?.validationStrategy() } private fun getEventDetails(): MutableList { @@ -427,7 +427,7 @@ class EventRepository( PeriodSelector( type = periodType, minDate = getPeriodMinDate(periodType), - maxDate = dateUtils.getStartOfDay(Date()), + maxDate = null, ) } else { null @@ -435,52 +435,56 @@ class EventRepository( } } + private fun D2.generatePeriod( + periodType: PeriodType, + date: Date = Date(), + offset: Int = 0, + ) = d2.periodModule().periodHelper() + .blockingGetPeriodForPeriodTypeAndDate(periodType, date, offset) + private fun getPeriodMinDate(periodType: PeriodType): Date? { - d2.programModule().programs() - .withTrackedEntityType() - .byUid().eq(programUid) - .one().blockingGet()?.let { program -> - val firstAvailablePeriodDate = getFirstAvailablePeriod(event?.enrollment(), programStage) - var minDate = dateUtils.expDate( - firstAvailablePeriodDate, - program.expiryDays() ?: 0, - periodType, - ) - val lastPeriodDate = dateUtils.getNextPeriod( - periodType, - minDate, - -1, - true, - ) + val program = programUid?.let { d2.program(it) } - if (lastPeriodDate.after( - dateUtils.getNextPeriod( - program.expiryPeriodType(), - minDate, - 0, - ), - ) - ) { - minDate = dateUtils.getNextPeriod(periodType, lastPeriodDate, 0) - } - return minDate + val expiryDays = program?.expiryDays() + + return if (expiryDays == null) { + null + } else { + val currentDate = Date() + val currentPeriod = d2.generatePeriod(periodType) + val previousPeriodLastDay = + d2.generatePeriod(PeriodType.Daily, currentPeriod.startDate()!!, expiryDays) + .startDate() + if (currentDate.after(previousPeriodLastDay)) { + currentDate + } else { + d2.generatePeriod(periodType, offset = -1).startDate() } - return null + } } - private fun getFirstAvailablePeriod(enrollmentUid: String?, programStage: ProgramStage?): Date { + /* private fun getFirstAvailablePeriod(enrollmentUid: String?, programStage: ProgramStage?): Date { val stageLastDate = getStageLastDate() val minEventDate = stageLastDate ?: when (programStage?.generatedByEnrollmentDate()) { true -> getEnrollmentDate(enrollmentUid) - else -> getEnrollmentIncidentDate(enrollmentUid) - ?: getEnrollmentDate(enrollmentUid) + else -> getEnrollmentIncidentDate(enrollmentUid) ?: getEnrollmentDate(enrollmentUid) } val calendar = DateUtils.getInstance().getCalendarByDate(minEventDate) - return dateUtils.getNextPeriod(programStage?.periodType(), calendar.time ?: event?.eventDate(), if (stageLastDate == null) 0 else 1) - } + return dateUtils.getNextPeriod( + */ + /* period = */ + /* programStage?.periodType(), + */ + /* currentDate = */ + /* calendar.time ?: event?.eventDate(), + */ + /* page = */ + /* if (stageLastDate == null) 0 else 1 + ) + }*/ - private fun getStageLastDate(): Date? { + /*private fun getStageLastDate(): Date? { val enrollmentUid = event?.enrollment() val programStageUid = programStage?.uid() val activeEvents = @@ -488,12 +492,14 @@ class EventRepository( .eq(enrollmentUid).byProgramStageUid() .eq(programStageUid) .byDeleted().isFalse - .orderByEventDate(RepositoryScope.OrderByDirection.DESC).blockingGet().filter { it.uid() != eventUid } + .orderByEventDate(RepositoryScope.OrderByDirection.DESC).blockingGet() + .filter { it.uid() != eventUid } val scheduleEvents = d2.eventModule().events().byEnrollmentUid().eq(enrollmentUid).byProgramStageUid() .eq(programStageUid) .byDeleted().isFalse - .orderByDueDate(RepositoryScope.OrderByDirection.DESC).blockingGet().filter { it.uid() != eventUid } + .orderByDueDate(RepositoryScope.OrderByDirection.DESC).blockingGet() + .filter { it.uid() != eventUid } var activeDate: Date? = null var scheduleDate: Date? = null @@ -518,7 +524,7 @@ class EventRepository( private fun getEnrollmentIncidentDate(uid: String?): Date? { val enrollment = d2.enrollmentModule().enrollments().uid(uid).blockingGet() return enrollment?.incidentDate() - } + }*/ private fun createEventDetailsSection(): FieldUiModel { return fieldFactory.createSection( @@ -640,7 +646,12 @@ class EventRepository( .orderBySortOrder(RepositoryScope.OrderByDirection.ASC).blockingGet() val metadataIconMap = - options.associate { it.uid() to metadataIconProvider(it.style(), defaultStyleColor) } + options.associate { + it.uid() to metadataIconProvider( + it.style(), + defaultStyleColor, + ) + } OptionSetConfiguration.OptionConfigData( options = options,