hot fix child transactiopns

This commit is contained in:
xds
2025-03-10 23:56:29 +03:00
parent ed8965b055
commit 4d5b89b08c
4 changed files with 66 additions and 61 deletions

View File

@@ -57,20 +57,22 @@ class FinancialService(
val budget = findProjectedBudget(
transaction.space!!.id!!, budgetId = null, transaction.date, transaction.date
)
val budgetCategory = budget.categories.firstOrNull { it.category.id == transaction.category.id }
?: throw NotFoundException("Budget category not found in the budget")
if (transaction.category.type.code == "INCOME") {
if (transaction.category.type.code == "EXPENSE") {
val budgetCategory = budget.categories.firstOrNull { it.category.id == transaction.category.id }
?: throw NotFoundException("Budget category not found in the budget")
if (transaction.category.type.code == "INCOME") {
budgetRepo.save(budget).awaitSingle()
}
val categorySums = getBudgetSumsByCategory(transaction.category.id!!, budget)
budgetCategory.currentPlanned = categorySums.getDouble("plannedAmount")
budgetCategory.currentSpent = categorySums.getDouble("instantAmount")
// При совпадении бюджетов разница просто корректирует лимит
if (transaction.type.code == "PLANNED") {
budgetCategory.currentLimit += transaction.amount
}
logger.info("updateBudgetOnCreate end")
budgetRepo.save(budget).awaitSingle()
}
val categorySums = getBudgetSumsByCategory(transaction.category.id!!, budget)
budgetCategory.currentPlanned = categorySums.getDouble("plannedAmount")
budgetCategory.currentSpent = categorySums.getDouble("instantAmount")
// При совпадении бюджетов разница просто корректирует лимит
if (transaction.type.code == "PLANNED") {
budgetCategory.currentLimit += transaction.amount
}
logger.info("updateBudgetOnCreate end")
budgetRepo.save(budget).awaitSingle()
}
@@ -147,26 +149,22 @@ class FinancialService(
oldBudget: Budget,
newBudget: Budget
) = coroutineScope {
val oldCategory = findBudgetCategory(oldTransaction, oldBudget)
val newCategory = findBudgetCategory(newTransaction, newBudget)
// val oldCategory = findBudgetCategory(oldTransaction, oldBudget)
// val newCategory = findBudgetCategory(newTransaction, newBudget)
if (oldCategory.category.id == newCategory.category.id) {
async {
updateBudgetCategory(oldTransaction, oldBudget, -difference)
updateBudgetCategory(newTransaction, newBudget, difference)
}
} else {
async {
updateBudgetCategory(
oldTransaction,
oldBudget,
-difference,
isCategoryChanged = true,
isOldCategory = true
)
updateBudgetCategory(newTransaction, newBudget, difference)
}
async {
updateBudgetCategory(
oldTransaction,
oldBudget,
-difference,
isCategoryChanged = true,
isOldCategory = true,
isNewBudget = true
)
updateBudgetCategory(newTransaction, newBudget, difference, isNewBudget = true)
}
}
private suspend fun findBudgetCategory(transaction: Transaction, budget: Budget): BudgetCategory {
@@ -202,7 +200,8 @@ class FinancialService(
budget: Budget,
difference: Double,
isCategoryChanged: Boolean = false,
isOldCategory: Boolean = false
isOldCategory: Boolean = false,
isNewBudget: Boolean = false
): Double {
return if (transaction.category.type.code == "EXPENSE") {
val sums = getBudgetSumsByCategory(transaction.category.id!!, budget)
@@ -215,6 +214,8 @@ class FinancialService(
if (isOldCategory) {
categoryBudget.currentLimit -= transaction.amount
} else categoryBudget.currentLimit += transaction.amount
} else if (isNewBudget) {
categoryBudget.currentLimit += transaction.amount
} else categoryBudget.currentLimit += difference
}
budgetRepo.save(budget).awaitSingle()