|
| 1 | +/* |
| 2 | + * Copyright 2026 Mifos Initiative |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | + * |
| 8 | + * See See https://github.com/openMF/kmp-project-template/blob/main/LICENSE |
| 9 | + */ |
| 10 | +package org.mifos.core.data.mapper.savings |
| 11 | + |
| 12 | +import kotlinx.datetime.LocalDate |
| 13 | +import org.mifos.core.model.savings.SavingDetail |
| 14 | +import org.mifos.core.model.savings.SavingDetailCurrency |
| 15 | +import org.mifos.core.model.savings.SavingDetailStatus |
| 16 | +import org.mifos.core.model.savings.SavingDetailSummary |
| 17 | +import org.mifos.core.model.savings.SavingDetailTimeline |
| 18 | +import org.mifos.core.model.savings.SavingPeriodType |
| 19 | +import org.mifos.core.network.fineract.savings.dto.SavingDetailCurrencyDto |
| 20 | +import org.mifos.core.network.fineract.savings.dto.SavingDetailDto |
| 21 | +import org.mifos.core.network.fineract.savings.dto.SavingDetailStatusDto |
| 22 | +import org.mifos.core.network.fineract.savings.dto.SavingDetailSummaryDto |
| 23 | +import org.mifos.core.network.fineract.savings.dto.SavingDetailTimelineDto |
| 24 | +import org.mifos.core.network.fineract.savings.dto.SavingPeriodTypeDto |
| 25 | + |
| 26 | +fun SavingDetailDto.toModel(): SavingDetail = |
| 27 | + SavingDetail( |
| 28 | + id = id, |
| 29 | + accountNo = accountNo, |
| 30 | + clientId = clientId, |
| 31 | + clientName = clientName, |
| 32 | + savingsProductId = savingsProductId, |
| 33 | + savingsProductName = savingsProductName, |
| 34 | + fieldOfficerId = fieldOfficerId, |
| 35 | + status = status?.toModel(), |
| 36 | + timeline = timeline?.toModel(), |
| 37 | + currency = currency?.toModel(), |
| 38 | + nominalAnnualInterestRate = nominalAnnualInterestRate, |
| 39 | + interestCompoundingPeriodType = interestCompoundingPeriodType?.toModel(), |
| 40 | + interestPostingPeriodType = interestPostingPeriodType?.toModel(), |
| 41 | + interestCalculationType = interestCalculationType?.toModel(), |
| 42 | + interestCalculationDaysInYearType = interestCalculationDaysInYearType?.toModel(), |
| 43 | + summary = summary?.toModel(), |
| 44 | + ) |
| 45 | + |
| 46 | +fun SavingDetailStatusDto.toModel(): SavingDetailStatus = |
| 47 | + SavingDetailStatus( |
| 48 | + id = id, |
| 49 | + code = code, |
| 50 | + value = value, |
| 51 | + submittedAndPendingApproval = submittedAndPendingApproval, |
| 52 | + approved = approved, |
| 53 | + rejected = rejected, |
| 54 | + withdrawnByApplicant = withdrawnByApplicant, |
| 55 | + active = active, |
| 56 | + closed = closed, |
| 57 | + ) |
| 58 | + |
| 59 | +fun SavingDetailTimelineDto.toModel(): SavingDetailTimeline = |
| 60 | + SavingDetailTimeline( |
| 61 | + submittedOnDate = submittedOnDate.toLocalDateOrNull(), |
| 62 | + ) |
| 63 | + |
| 64 | +fun SavingDetailCurrencyDto.toModel(): SavingDetailCurrency = |
| 65 | + SavingDetailCurrency( |
| 66 | + code = code, |
| 67 | + name = name, |
| 68 | + decimalPlaces = decimalPlaces, |
| 69 | + displaySymbol = displaySymbol, |
| 70 | + nameCode = nameCode, |
| 71 | + displayLabel = displayLabel, |
| 72 | + ) |
| 73 | + |
| 74 | +fun SavingPeriodTypeDto.toModel(): SavingPeriodType = |
| 75 | + SavingPeriodType( |
| 76 | + id = id, |
| 77 | + code = code, |
| 78 | + value = value, |
| 79 | + ) |
| 80 | + |
| 81 | +fun SavingDetailSummaryDto.toModel(): SavingDetailSummary = |
| 82 | + SavingDetailSummary( |
| 83 | + currency = currency?.toModel(), |
| 84 | + accountBalance = accountBalance, |
| 85 | + availableBalance = availableBalance, |
| 86 | + ) |
| 87 | + |
| 88 | +private fun List<Int>?.toLocalDateOrNull(): LocalDate? { |
| 89 | + if (this == null || this.size < 3) return null |
| 90 | + return try { |
| 91 | + LocalDate(this[0], this[1], this[2]) |
| 92 | + } catch (e: Exception) { |
| 93 | + null |
| 94 | + } |
| 95 | +} |
0 commit comments