Skip to content

Commit

Permalink
Allow QIF input of payee records without amount
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed Jan 7, 2025
1 parent 8e8c2f2 commit a5ce08d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.totschnig.myexpenses.dialog.ProgressDialogFragment
import org.totschnig.myexpenses.dialog.QifImportDialogFragment
import org.totschnig.myexpenses.export.qif.QifDateFormat
import org.totschnig.myexpenses.injector
import org.totschnig.myexpenses.util.crashreporting.CrashHandler
import org.totschnig.myexpenses.util.safeMessage
import org.totschnig.myexpenses.viewmodel.QifImportViewModel

Expand Down Expand Up @@ -55,7 +56,7 @@ class QifImport : ProtectedFragmentActivity() {
mUri: Uri,
qifDateFormat: QifDateFormat,
accountId: Long,
currency: String?,
currency: String,
withTransactions: Boolean,
withCategories: Boolean,
withParties: Boolean,
Expand All @@ -72,9 +73,10 @@ class QifImport : ProtectedFragmentActivity() {
), PROGRESS_TAG
)
.commitNow()
importViewModel.importData(mUri, qifDateFormat, accountId, currencyContext[currency!!], withTransactions,
importViewModel.importData(mUri, qifDateFormat, accountId, currencyContext[currency], withTransactions,
withCategories, withParties, encoding, autoFillCategories).observe(this) {
it.onFailure {
CrashHandler.report(it)
progressDialogFragment?.appendToMessage(it.safeMessage)
}
progressDialogFragment?.onTaskCompleted()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.totschnig.myexpenses.io.ImportAccount.Builder
import org.totschnig.myexpenses.io.ImportTransaction
import org.totschnig.myexpenses.model.CurrencyUnit
import java.io.IOException
import java.math.BigDecimal

/**
* Created by IntelliJ IDEA.
Expand Down Expand Up @@ -102,7 +103,7 @@ class QifParser(
do {
val t = readTransaction(r, dateFormat, currency)
if (t.isOpeningBalance) {
account.openingBalance(t.amount)
account.openingBalance(t.amount ?: BigDecimal.ZERO)
t.toAccount.takeIf { !it.isNullOrEmpty() }?.let {
account.memo(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ data class ImportAccount(
memo = memo ?: "",
desc = desc ?: "",
openingBalance = openingBalance ?: BigDecimal.ZERO,
transactions = transactions.map { it.build() }
transactions = transactions.mapNotNull { it.build() }
)
}

Expand All @@ -74,7 +74,7 @@ data class ImportTransaction(
class Builder {
private var date: Date = Date()
private var valueDate: Date? = null
lateinit var amount: BigDecimal
var amount: BigDecimal? = null
var payee: String? = null
private var memo: String? = null
var category: String? = null
Expand Down Expand Up @@ -106,22 +106,24 @@ data class ImportTransaction(

val isOpeningBalance get() = payee == "Opening Balance"

fun build(): ImportTransaction = ImportTransaction(
date,
valueDate,
amount,
payee,
memo,
category,
categoryClass,
toAccount,
toAmount,
status,
number,
method,
if (tags.isEmpty()) null else tags.toImmutableList(),
if (splits.isEmpty()) null else splits.map { split -> split.build() }
)
fun build(): ImportTransaction? = amount?.let {
ImportTransaction(
date,
valueDate,
it,
payee,
memo,
category,
categoryClass,
toAccount,
toAmount,
status,
number,
method,
if (tags.isEmpty()) null else tags.toImmutableList(),
if (splits.isEmpty()) null else splits.mapNotNull { split -> split.build() }
)
}
}

val isSplit get() = splits != null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.totschnig.myexpenses.export.qif

import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.totschnig.myexpenses.io.ImportAccount
Expand All @@ -21,7 +20,7 @@ class QifReduceTransfersTest {
ImportTransaction.Builder()
.toAccount(account2)
.date(now)
.amount(BigDecimal(-5)).build()
.amount(BigDecimal(-5)).build()!!
))
val reduced = QifUtils.reduceTransfers(listOf(fromAccount))
assertThat(reduced[0].transactions.size).isEqualTo(1)
Expand All @@ -41,7 +40,7 @@ class QifReduceTransfersTest {
.toAccount(account2)
.date(now)
.amount(BigDecimal(-5)))
.build()
.build()!!
))
val reduced = QifUtils.reduceTransfers(listOf(fromAccount))
assertThat(reduced[0].transactions.size).isEqualTo(1)
Expand All @@ -58,14 +57,14 @@ class QifReduceTransfersTest {
ImportTransaction.Builder()
.toAccount(account2)
.date(now)
.amount(BigDecimal(-5)).build()
.amount(BigDecimal(-5)).build()!!
))
val toAccount = ImportAccount(memo = account2,
transactions = listOf(
ImportTransaction.Builder()
.toAccount(account1)
.date(now)
.amount(BigDecimal(5)).build()
.amount(BigDecimal(5)).build()!!
))
val reduced = QifUtils.reduceTransfers(listOf(fromAccount, toAccount))
assertThat(reduced[0].transactions.size).isEqualTo(0)
Expand All @@ -86,14 +85,14 @@ class QifReduceTransfersTest {
.toAccount(account2)
.date(now)
.amount(BigDecimal(-5)))
.build()
.build()!!
))
val toAccount = ImportAccount(memo = account2,
transactions = listOf(
ImportTransaction.Builder()
.toAccount(account1)
.date(now)
.amount(BigDecimal(5)).build()
.amount(BigDecimal(5)).build()!!
))
val reduced = QifUtils.reduceTransfers(listOf(fromAccount, toAccount))
assertThat(reduced[0].transactions.size).isEqualTo(1)
Expand All @@ -116,14 +115,14 @@ class QifReduceTransfersTest {
.toAccount(account2)
.date(now)
.amount(BigDecimal(5)))
.build()
.build()!!
))
val toAccount = ImportAccount(memo = account2,
transactions = listOf(
ImportTransaction.Builder()
.toAccount(account1)
.date(now)
.amount(BigDecimal(-5)).build()
.amount(BigDecimal(-5)).build()!!
))
val reduced = QifUtils.reduceTransfers(listOf(fromAccount, toAccount))
assertThat(reduced[0].transactions.size).isEqualTo(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class QifUtilTwoSidesOfTheSameTransferTest : TestCase() {
.toAccount(toAccount.memo)
.date(now)
.amount(BigDecimal(5))
.build()
.build()!!
val toTransaction = ImportTransaction.Builder()
.toAccount(fromAccount.memo)
.date(now)
.amount(BigDecimal(-5))
.build()
.build()!!
Truth.assertThat(twoSidesOfTheSameTransfer(
fromAccount,
fromTransaction,
Expand All @@ -41,12 +41,12 @@ class QifUtilTwoSidesOfTheSameTransferTest : TestCase() {
.toAccount(toAccount.memo)
.date(now)
.amount(BigDecimal(5))
.build()
.build()!!
val toTransaction = ImportTransaction.Builder()
.toAccount(fromAccount.memo)
.date(Date(System.currentTimeMillis() - 100000))
.amount(BigDecimal(-5))
.build()
.build()!!
Truth.assertThat(twoSidesOfTheSameTransfer(
fromAccount,
fromTransaction,
Expand All @@ -63,12 +63,12 @@ class QifUtilTwoSidesOfTheSameTransferTest : TestCase() {
.toAccount(toAccount.memo)
.date(now)
.amount(BigDecimal(5))
.build()
.build()!!
val toTransaction = ImportTransaction.Builder()
.toAccount("Konto 3")
.date(now)
.amount(BigDecimal(-5))
.build()
.build()!!
Truth.assertThat(twoSidesOfTheSameTransfer(
fromAccount,
fromTransaction,
Expand Down

0 comments on commit a5ce08d

Please sign in to comment.