Skip to content

Commit

Permalink
periods not being generated properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Balcan committed Dec 4, 2024
1 parent c1dd07e commit 680abfd
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions form/src/main/java/org/dhis2/form/data/EventRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<FieldUiModel> {
Expand Down Expand Up @@ -427,73 +427,79 @@ class EventRepository(
PeriodSelector(
type = periodType,
minDate = getPeriodMinDate(periodType),
maxDate = dateUtils.getStartOfDay(Date()),
maxDate = null,
)
} else {
null
}
}
}

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 =
d2.eventModule().events().byEnrollmentUid()
.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
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 680abfd

Please sign in to comment.