fix limit increase when not planned
This commit is contained in:
@@ -50,46 +50,17 @@ class FinancialService(
|
||||
}
|
||||
return@flatMap getBudgetSumsByCategory(transaction.category.id!!, budget)
|
||||
.flatMap { sums ->
|
||||
|
||||
budgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0
|
||||
budgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0
|
||||
// При совпадении бюджетов разница просто корректирует лимит
|
||||
budgetCategory.currentLimit += transaction.amount
|
||||
logger.info("updateBudgetOnEdit end")
|
||||
if (transaction.type.code == "PLANNED") {
|
||||
budgetCategory.currentLimit += transaction.amount
|
||||
}
|
||||
logger.info("updateBudgetOnCreate end")
|
||||
budgetRepo.save(budget).then()
|
||||
}
|
||||
// getBudgetCategories(budget.dateFrom, budget.dateTo).flatMap { categories ->
|
||||
// val updatedCategoriesMono: Mono<List<BudgetCategory>> = when (transaction.type.code) {
|
||||
// "PLANNED" -> Flux.fromIterable(budget.categories)
|
||||
// .map { category ->
|
||||
// if (category.category.id == transaction.category.id) {
|
||||
// categories[category.category.id]?.let { data ->
|
||||
// category.currentSpent = data["instantAmount"] ?: 0.0
|
||||
// category.currentPlanned = data["plannedAmount"] ?: 0.0
|
||||
// category.currentLimit += transaction.amount
|
||||
// }
|
||||
// }
|
||||
// category
|
||||
// }.collectList()
|
||||
//
|
||||
// "INSTANT" -> Flux.fromIterable(budget.categories)
|
||||
// .map { category ->
|
||||
// if (category.category.id == transaction.category.id) {
|
||||
// categories[category.category.id]?.let { data ->
|
||||
// category.currentSpent = data["instantAmount"] ?: 0.0
|
||||
// category.currentPlanned = data["plannedAmount"] ?: 0.0
|
||||
// }
|
||||
// }
|
||||
// category
|
||||
// }.collectList()
|
||||
//
|
||||
// else -> Mono.just(budget.categories) // Добавляем обработку типа по умолчанию
|
||||
// }
|
||||
//
|
||||
// updatedCategoriesMono.flatMap { updated ->
|
||||
// budget.categories = updated.toMutableList()
|
||||
// budgetRepo.save(budget).then() // Гарантируем завершение сохранения
|
||||
// }
|
||||
// }
|
||||
}.then() // Возвращаем корректный Mono<Void>
|
||||
}
|
||||
|
||||
@@ -132,7 +103,9 @@ class FinancialService(
|
||||
budgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0
|
||||
budgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0
|
||||
// При совпадении бюджетов разница просто корректирует лимит
|
||||
budgetCategory.currentLimit += difference
|
||||
if (newTransaction.type.code == "PLANNED") {
|
||||
budgetCategory.currentLimit += difference
|
||||
}
|
||||
logger.info("updateBudgetOnEdit end")
|
||||
budgetRepo.save(newBudget).then()
|
||||
}
|
||||
@@ -151,7 +124,9 @@ class FinancialService(
|
||||
oldBudgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0
|
||||
oldBudgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0
|
||||
// В старом бюджете вычитаем разницу, так как транзакция перемещается
|
||||
oldBudgetCategory.currentLimit -= difference
|
||||
if (oldTransaction.type.code == "PLANNED") {
|
||||
oldBudgetCategory.currentLimit -= difference
|
||||
}
|
||||
budgetRepo.save(oldBudget).then()
|
||||
}
|
||||
}
|
||||
@@ -164,7 +139,9 @@ class FinancialService(
|
||||
newBudgetCategory.currentPlanned = sums.getDouble("plannedAmount") ?: 0.0
|
||||
newBudgetCategory.currentSpent = sums.getDouble("instantAmount") ?: 0.0
|
||||
// В новом бюджете прибавляем разницу
|
||||
newBudgetCategory.currentLimit += difference
|
||||
if (newTransaction.type.code == "PLANNED") {
|
||||
newBudgetCategory.currentLimit += difference
|
||||
}
|
||||
budgetRepo.save(newBudget).then()
|
||||
}
|
||||
}
|
||||
@@ -214,7 +191,6 @@ class FinancialService(
|
||||
}.then() // Возвращаем корректный Mono<Void>
|
||||
}
|
||||
|
||||
|
||||
@Cacheable("budgetsList")
|
||||
fun getBudgets(sortSetting: SortSetting? = null): Mono<MutableList<Budget>> {
|
||||
val sort = if (sortSetting != null) {
|
||||
@@ -227,7 +203,6 @@ class FinancialService(
|
||||
.collectList() // Сбор Flux<Budget> в Mono<List<Budget>>
|
||||
}
|
||||
|
||||
|
||||
// @Cacheable("budgets", key = "#id")
|
||||
fun getBudget(id: String): Mono<BudgetDTO> {
|
||||
return budgetRepo.findById(id)
|
||||
@@ -279,7 +254,6 @@ class FinancialService(
|
||||
.switchIfEmpty(Mono.error(BudgetNotFoundException("Budget not found with id: $id")))
|
||||
}
|
||||
|
||||
|
||||
fun regenCats(): Mono<Void> {
|
||||
return budgetRepo.findAll()
|
||||
.flatMap { budget ->
|
||||
@@ -337,7 +311,6 @@ class FinancialService(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getBudgetByDate(date: LocalDate): Mono<Budget> {
|
||||
return budgetRepo.findByDateFromLessThanEqualAndDateToGreaterThanEqual(date, date).switchIfEmpty(Mono.empty())
|
||||
}
|
||||
@@ -361,7 +334,6 @@ class FinancialService(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getBudgetTransactionsByType(budgetId: String): Mono<Map<String, List<Transaction>>> {
|
||||
return budgetRepo.findById(budgetId).flatMap { it ->
|
||||
getTransactionsByTypes(it.dateFrom, it.dateTo)
|
||||
@@ -387,7 +359,6 @@ class FinancialService(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@CacheEvict(cacheNames = ["budgets", "budgetsList"], allEntries = true)
|
||||
fun setCategoryLimit(budgetId: String, catId: String, limit: Double): Mono<BudgetCategory> {
|
||||
return budgetRepo.findById(budgetId).flatMap { budget ->
|
||||
@@ -408,7 +379,6 @@ class FinancialService(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getWarns(budgetId: String, isHide: Boolean? = null): Mono<List<Warn>> {
|
||||
return warnRepo.findAllByBudgetIdAndIsHide(budgetId, isHide == true).collectList()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user