From 2ebe3a82836ffc85bd35de8d22e49a6af15a13f8 Mon Sep 17 00:00:00 2001 From: Bilal Khan Date: Thu, 30 May 2024 18:10:51 +0500 Subject: [PATCH] worked on the functionality to update the budget amount to make it equal to the new updating total amount --- cmd/root.go | 2 +- .../db/total_amount_db/total_amount_db.go | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index a8ffd65..0c878cf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,7 +9,7 @@ import ( "github.com/spf13/cobra" ) -const version = "v0.1.122" +const version = "v0.1.123" // rootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ diff --git a/framework/db/total_amount_db/total_amount_db.go b/framework/db/total_amount_db/total_amount_db.go index 66c2edb..e508e83 100644 --- a/framework/db/total_amount_db/total_amount_db.go +++ b/framework/db/total_amount_db/total_amount_db.go @@ -173,11 +173,26 @@ func (h MyTotalAmountDB) UpdateTotalAmount(tv *entities.TotalAmountVariables) er remainingAmountInDB += updatedRemaining } else if tv.TotalAmount < totalAmountInDB { // Update Spent Amount and Remaining Amount if Total Amount decreases - if spentAmountInDB <= tv.TotalAmount { - remainingAmountInDB = tv.TotalAmount - spentAmountInDB + _, budgetAmount, err := h.Deps.ManageBudget.TakeBudgetAmount() + if err != nil { + return nil + } + + totalBudgetAmount := 0 + for _, amount := range budgetAmount { + totalBudgetAmount += amount + } + + // if entered amount is greater than the total budget amount + if tv.TotalAmount >= totalBudgetAmount { + if spentAmountInDB <= tv.TotalAmount { + remainingAmountInDB = tv.TotalAmount - spentAmountInDB + } else { + spentAmountInDB = 0 + remainingAmountInDB = 0 + } } else { - spentAmountInDB = 0 - remainingAmountInDB = 0 + return errors.New("update the budget amount to make it equal to your new amount") } } else { return errors.New("this amount is already present. enter a different amount")