diff --git a/app/Api/V1/Controllers/AvailableBudgetController.php b/app/Api/V1/Controllers/AvailableBudgetController.php index 5747dd8ca9..30fbb0cb2e 100644 --- a/app/Api/V1/Controllers/AvailableBudgetController.php +++ b/app/Api/V1/Controllers/AvailableBudgetController.php @@ -214,7 +214,7 @@ class AvailableBudgetController extends Controller $data['currency_id'] = $currency->id; - $this->repository->updateAvailableBudget($availableBudget, $data); + $this->abRepository->updateAvailableBudget($availableBudget, $data); $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); diff --git a/app/Api/V1/Controllers/BudgetController.php b/app/Api/V1/Controllers/BudgetController.php index ba96bcf6b8..2577c6e5b3 100644 --- a/app/Api/V1/Controllers/BudgetController.php +++ b/app/Api/V1/Controllers/BudgetController.php @@ -233,7 +233,7 @@ class BudgetController extends Controller { $data = $request->getAll(); $data['budget'] = $budget; - $budgetLimit = $this->repository->storeBudgetLimit($data); + $budgetLimit = $this->blRepository->storeBudgetLimit($data); $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); diff --git a/app/Api/V1/Controllers/BudgetLimitController.php b/app/Api/V1/Controllers/BudgetLimitController.php index 5c5142aaaa..7c14d92421 100644 --- a/app/Api/V1/Controllers/BudgetLimitController.php +++ b/app/Api/V1/Controllers/BudgetLimitController.php @@ -178,7 +178,7 @@ class BudgetLimitController extends Controller throw new FireflyException('Unknown budget.'); } $data['budget'] = $budget; - $budgetLimit = $this->repository->storeBudgetLimit($data); + $budgetLimit = $this->blRepository->storeBudgetLimit($data); $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); @@ -259,7 +259,7 @@ class BudgetLimitController extends Controller { $data = $request->getAll(); $data['budget'] = $budgetLimit->budget; - $budgetLimit = $this->repository->updateBudgetLimit($budgetLimit, $data); + $budgetLimit = $this->blRepository->updateBudgetLimit($budgetLimit, $data); $manager = new Manager; $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; $manager->setSerializer(new JsonApiSerializer($baseUrl)); diff --git a/app/Http/Controllers/Budget/AmountController.php b/app/Http/Controllers/Budget/AmountController.php index 9e9bb68473..de35b520e5 100644 --- a/app/Http/Controllers/Budget/AmountController.php +++ b/app/Http/Controllers/Budget/AmountController.php @@ -29,6 +29,7 @@ use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\BudgetIncomeRequest; use FireflyIII\Models\Budget; use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface; +use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\OperationsRepositoryInterface; use FireflyIII\Support\Http\Controllers\DateCalculation; @@ -46,6 +47,8 @@ class AmountController extends Controller /** @var AvailableBudgetRepositoryInterface */ private $abRepository; + /** @var BudgetLimitRepositoryInterface */ + private $blRepository; /** @var OperationsRepositoryInterface */ private $opsRepository; /** @var BudgetRepositoryInterface The budget repository */ @@ -67,6 +70,7 @@ class AmountController extends Controller $this->repository = app(BudgetRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class); $this->abRepository = app(AvailableBudgetRepositoryInterface::class); + $this->blRepository = app(BudgetLimitRepositoryInterface::class); return $next($request); } @@ -99,7 +103,7 @@ class AmountController extends Controller $periodLength = $start->diffInDays($end) + 1; // absolute period length. // update limit amount: - $budgetLimit = $this->repository->updateLimitAmount($budget, $start, $end, $amount); + $budgetLimit = $this->blRepository->updateLimitAmount($budget, $start, $end, $amount); // calculate what the user has spent in current period. $spent = $this->opsRepository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end); diff --git a/app/Repositories/Budget/AvailableBudgetRepository.php b/app/Repositories/Budget/AvailableBudgetRepository.php index 3e6d4053c8..9ed3832d74 100644 --- a/app/Repositories/Budget/AvailableBudgetRepository.php +++ b/app/Repositories/Budget/AvailableBudgetRepository.php @@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use Exception; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; @@ -175,5 +176,34 @@ class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface $this->user = $user; } + /** + * @param AvailableBudget $availableBudget + * @param array $data + * + * @return AvailableBudget + * @throws FireflyException + */ + public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget + { + $existing = $this->user->availableBudgets() + ->where('transaction_currency_id', $data['currency_id']) + ->where('start_date', $data['start']->format('Y-m-d 00:00:00')) + ->where('end_date', $data['end']->format('Y-m-d 00:00:00')) + ->where('id', '!=', $availableBudget->id) + ->first(); + + if (null !== $existing) { + throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id)); + } + $availableBudget->transaction_currency_id = $data['currency_id']; + $availableBudget->start_date = $data['start']; + $availableBudget->end_date = $data['end']; + $availableBudget->amount = $data['amount']; + $availableBudget->save(); + + return $availableBudget; + + } + } \ No newline at end of file diff --git a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php index ab58cc9846..dcdefd9818 100644 --- a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php +++ b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php @@ -65,17 +65,6 @@ interface AvailableBudgetRepositoryInterface */ public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection; - /** - * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end - * @param string $amount - * - * @return AvailableBudget - */ - public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget; - - /** * Returns all available budget objects. * @@ -87,9 +76,27 @@ interface AvailableBudgetRepositoryInterface */ public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection; + /** + * @param TransactionCurrency $currency + * @param Carbon $start + * @param Carbon $end + * @param string $amount + * + * @return AvailableBudget + */ + public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget; + /** * @param User $user */ public function setUser(User $user): void; + /** + * @param AvailableBudget $availableBudget + * @param array $data + * + * @return AvailableBudget + */ + public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget; + } \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetLimitRepository.php b/app/Repositories/Budget/BudgetLimitRepository.php index a9b3a4f4df..9ca5ace1ea 100644 --- a/app/Repositories/Budget/BudgetLimitRepository.php +++ b/app/Repositories/Budget/BudgetLimitRepository.php @@ -26,6 +26,7 @@ namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use Exception; +use FireflyIII\Factory\TransactionCurrencyFactory; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\TransactionCurrency; @@ -230,4 +231,150 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface { $this->user = $user; } + + /** + * @param array $data + * + * @return BudgetLimit + */ + public function storeBudgetLimit(array $data): BudgetLimit + { + /** @var Budget $budget */ + $budget = $data['budget']; + + // if no currency has been provided, use the user's default currency: + /** @var TransactionCurrencyFactory $factory */ + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); + if (null === $currency) { + $currency = app('amount')->getDefaultCurrencyByUser($this->user); + } + + // find limit with same date range. + // if it exists, return that one. + $limit = $budget->budgetlimits() + ->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00')) + ->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00')) + ->where('budget_limits.transaction_currency_id', $currency->id) + ->get(['budget_limits.*'])->first(); + if (null !== $limit) { + return $limit; + } + Log::debug('No existing budget limit, create a new one'); + + // or create one and return it. + $limit = new BudgetLimit; + $limit->budget()->associate($budget); + $limit->start_date = $data['start']->format('Y-m-d 00:00:00'); + $limit->end_date = $data['end']->format('Y-m-d 00:00:00'); + $limit->amount = $data['amount']; + $limit->transaction_currency_id = $currency->id; + $limit->save(); + Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount'])); + + return $limit; + } + + /** + * @param BudgetLimit $budgetLimit + * @param array $data + * + * @return BudgetLimit + * @throws Exception + */ + public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit + { + /** @var Budget $budget */ + $budget = $data['budget']; + + $budgetLimit->budget()->associate($budget); + $budgetLimit->start_date = $data['start']->format('Y-m-d 00:00:00'); + $budgetLimit->end_date = $data['end']->format('Y-m-d 00:00:00'); + $budgetLimit->amount = $data['amount']; + + // if no currency has been provided, use the user's default currency: + /** @var TransactionCurrencyFactory $factory */ + $factory = app(TransactionCurrencyFactory::class); + $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); + if (null === $currency) { + $currency = app('amount')->getDefaultCurrencyByUser($this->user); + } + $currency->enabled = true; + $currency->save(); + $budgetLimit->transaction_currency_id = $currency->id; + + $budgetLimit->save(); + Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount'])); + + return $budgetLimit; + } + + /** + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param string $amount + * + * @return BudgetLimit|null + * + */ + public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit + { + // count the limits: + $limits = $budget->budgetlimits() + ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) + ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) + ->get(['budget_limits.*'])->count(); + Log::debug(sprintf('Found %d budget limits.', $limits)); + + // there might be a budget limit for these dates: + /** @var BudgetLimit $limit */ + $limit = $budget->budgetlimits() + ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) + ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) + ->first(['budget_limits.*']); + + // if more than 1 limit found, delete the others: + if ($limits > 1 && null !== $limit) { + Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id)); + $budget->budgetlimits() + ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) + ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) + ->where('budget_limits.id', '!=', $limit->id)->delete(); + } + + // delete if amount is zero. + // Returns 0 if the two operands are equal, + // 1 if the left_operand is larger than the right_operand, -1 otherwise. + if (null !== $limit && bccomp($amount, '0') <= 0) { + Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id)); + try { + $limit->delete(); + } catch (Exception $e) { + Log::debug(sprintf('Could not delete limit: %s', $e->getMessage())); + } + + + return null; + } + // update if exists: + if (null !== $limit) { + Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount)); + $limit->amount = $amount; + $limit->save(); + + return $limit; + } + Log::debug('No existing budget limit, create a new one'); + // or create one and return it. + $limit = new BudgetLimit; + $limit->budget()->associate($budget); + $limit->start_date = $start->startOfDay(); + $limit->end_date = $end->startOfDay(); + $limit->amount = $amount; + $limit->save(); + Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount)); + + return $limit; + } } \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php index 92cdd67e79..7e4d249e68 100644 --- a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php @@ -75,4 +75,29 @@ interface BudgetLimitRepositoryInterface */ public function setUser(User $user): void; + /** + * @param array $data + * + * @return BudgetLimit + */ + public function storeBudgetLimit(array $data): BudgetLimit; + + /** + * @param BudgetLimit $budgetLimit + * @param array $data + * + * @return BudgetLimit + */ + public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit; + + /** + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param string $amount + * + * @return BudgetLimit|null + */ + public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit; + } \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 0265d6b8fa..167ff56fd0 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -24,21 +24,14 @@ namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; use Exception; -use FireflyIII\Exceptions\FireflyException; -use FireflyIII\Factory\TransactionCurrencyFactory; -use FireflyIII\Helpers\Collector\GroupCollectorInterface; -use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\RuleAction; use FireflyIII\Models\RuleTrigger; -use FireflyIII\Models\TransactionCurrency; -use FireflyIII\Models\TransactionType; use FireflyIII\Services\Internal\Destroy\BudgetDestroyService; use FireflyIII\User; use Illuminate\Support\Collection; use Log; -use Navigation; /** * Class BudgetRepository. @@ -294,50 +287,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $newBudget; } - /** - * @param array $data - * - * @return BudgetLimit - */ - public function storeBudgetLimit(array $data): BudgetLimit - { - $this->cleanupBudgets(); - /** @var Budget $budget */ - $budget = $data['budget']; - - // if no currency has been provided, use the user's default currency: - /** @var TransactionCurrencyFactory $factory */ - $factory = app(TransactionCurrencyFactory::class); - $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); - if (null === $currency) { - $currency = app('amount')->getDefaultCurrencyByUser($this->user); - } - - // find limit with same date range. - // if it exists, return that one. - $limit = $budget->budgetlimits() - ->where('budget_limits.start_date', $data['start']->format('Y-m-d 00:00:00')) - ->where('budget_limits.end_date', $data['end']->format('Y-m-d 00:00:00')) - ->where('budget_limits.transaction_currency_id', $currency->id) - ->get(['budget_limits.*'])->first(); - if (null !== $limit) { - return $limit; - } - Log::debug('No existing budget limit, create a new one'); - - // or create one and return it. - $limit = new BudgetLimit; - $limit->budget()->associate($budget); - $limit->start_date = $data['start']->format('Y-m-d 00:00:00'); - $limit->end_date = $data['end']->format('Y-m-d 00:00:00'); - $limit->amount = $data['amount']; - $limit->transaction_currency_id = $currency->id; - $limit->save(); - Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $data['amount'])); - - return $limit; - } - /** * @param Budget $budget * @param array $data @@ -357,142 +306,6 @@ class BudgetRepository implements BudgetRepositoryInterface return $budget; } - /** - * TODO only used in the API. - * - * @param AvailableBudget $availableBudget - * @param array $data - * - * @return AvailableBudget - * @throws FireflyException - */ - public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget - { - $existing = $this->user->availableBudgets() - ->where('transaction_currency_id', $data['currency_id']) - ->where('start_date', $data['start']->format('Y-m-d 00:00:00')) - ->where('end_date', $data['end']->format('Y-m-d 00:00:00')) - ->where('id', '!=', $availableBudget->id) - ->first(); - - if (null !== $existing) { - throw new FireflyException(sprintf('An entry already exists for these parameters: available budget object with ID #%d', $existing->id)); - } - $availableBudget->transaction_currency_id = $data['currency_id']; - $availableBudget->start_date = $data['start']; - $availableBudget->end_date = $data['end']; - $availableBudget->amount = $data['amount']; - $availableBudget->save(); - - return $availableBudget; - - } - - /** - * @param BudgetLimit $budgetLimit - * @param array $data - * - * @return BudgetLimit - * @throws Exception - */ - public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit - { - $this->cleanupBudgets(); - /** @var Budget $budget */ - $budget = $data['budget']; - - $budgetLimit->budget()->associate($budget); - $budgetLimit->start_date = $data['start']->format('Y-m-d 00:00:00'); - $budgetLimit->end_date = $data['end']->format('Y-m-d 00:00:00'); - $budgetLimit->amount = $data['amount']; - - // if no currency has been provided, use the user's default currency: - /** @var TransactionCurrencyFactory $factory */ - $factory = app(TransactionCurrencyFactory::class); - $currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null); - if (null === $currency) { - $currency = app('amount')->getDefaultCurrencyByUser($this->user); - } - $currency->enabled = true; - $currency->save(); - $budgetLimit->transaction_currency_id = $currency->id; - - $budgetLimit->save(); - Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount'])); - - return $budgetLimit; - } - - /** - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param string $amount - * - * @return BudgetLimit|null - * - */ - public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit - { - $this->cleanupBudgets(); - // count the limits: - $limits = $budget->budgetlimits() - ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) - ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) - ->get(['budget_limits.*'])->count(); - Log::debug(sprintf('Found %d budget limits.', $limits)); - - // there might be a budget limit for these dates: - /** @var BudgetLimit $limit */ - $limit = $budget->budgetlimits() - ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) - ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) - ->first(['budget_limits.*']); - - // if more than 1 limit found, delete the others: - if ($limits > 1 && null !== $limit) { - Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id)); - $budget->budgetlimits() - ->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00')) - ->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00')) - ->where('budget_limits.id', '!=', $limit->id)->delete(); - } - - // delete if amount is zero. - // Returns 0 if the two operands are equal, - // 1 if the left_operand is larger than the right_operand, -1 otherwise. - if (null !== $limit && bccomp($amount, '0') <= 0) { - Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id)); - try { - $limit->delete(); - } catch (Exception $e) { - Log::debug(sprintf('Could not delete limit: %s', $e->getMessage())); - } - - - return null; - } - // update if exists: - if (null !== $limit) { - Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount)); - $limit->amount = $amount; - $limit->save(); - - return $limit; - } - Log::debug('No existing budget limit, create a new one'); - // or create one and return it. - $limit = new BudgetLimit; - $limit->budget()->associate($budget); - $limit->start_date = $start->startOfDay(); - $limit->end_date = $end->startOfDay(); - $limit->amount = $amount; - $limit->save(); - Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount)); - - return $limit; - } - /** * @param string $oldName * @param string $newName diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 2264ffd8e3..4597bd1599 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -23,10 +23,7 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; -use FireflyIII\Models\AvailableBudget; use FireflyIII\Models\Budget; -use FireflyIII\Models\BudgetLimit; -use FireflyIII\Models\TransactionCurrency; use FireflyIII\User; use Illuminate\Support\Collection; @@ -125,16 +122,6 @@ interface BudgetRepositoryInterface */ public function setUser(User $user); - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array; - - /** * @param array $data * @@ -142,13 +129,6 @@ interface BudgetRepositoryInterface */ public function store(array $data): Budget; - /** - * @param array $data - * - * @return BudgetLimit - */ - public function storeBudgetLimit(array $data): BudgetLimit; - /** * @param Budget $budget * @param array $data @@ -156,31 +136,4 @@ interface BudgetRepositoryInterface * @return Budget */ public function update(Budget $budget, array $data): Budget; - - /** - * @param AvailableBudget $availableBudget - * @param array $data - * - * @return AvailableBudget - */ - public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget; - - /** - * @param BudgetLimit $budgetLimit - * @param array $data - * - * @return BudgetLimit - */ - public function updateBudgetLimit(BudgetLimit $budgetLimit, array $data): BudgetLimit; - - - /** - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param string $amount - * - * @return BudgetLimit|null - */ - public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): ?BudgetLimit; } diff --git a/app/Repositories/Budget/NoBudgetRepository.php b/app/Repositories/Budget/NoBudgetRepository.php index 64cfea57b4..28edbcc594 100644 --- a/app/Repositories/Budget/NoBudgetRepository.php +++ b/app/Repositories/Budget/NoBudgetRepository.php @@ -52,58 +52,6 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface } } - - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array - { - /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); - - $collector->setUser($this->user); - $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget(); - - if ($accounts->count() > 0) { - $collector->setAccounts($accounts); - } - $journals = $collector->getExtractedJournals(); - $return = []; - $total = []; - $currencies = []; - /** @var array $journal */ - foreach ($journals as $journal) { - $code = $journal['currency_code']; - if (!isset($currencies[$code])) { - $currencies[$code] = [ - 'id' => $journal['currency_id'], - 'name' => $journal['currency_name'], - 'symbol' => $journal['currency_symbol'], - 'decimal_places' => $journal['currency_decimal_places'], - ]; - } - $total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount']; - } - foreach ($total as $code => $spent) { - /** @var TransactionCurrency $currency */ - $currency = $currencies[$code]; - $return[] = [ - 'currency_id' => $currency['id'], - 'currency_code' => $code, - 'currency_name' => $currency['name'], - 'currency_symbol' => $currency['symbol'], - 'currency_decimal_places' => $currency['decimal_places'], - 'amount' => $spent, - ]; - } - - return $return; - } - /** * @param Collection $accounts * @param Carbon $start @@ -157,4 +105,56 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface { $this->user = $user; } + + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + * @deprecated + */ + public function spentInPeriodWoBudgetMc(Collection $accounts, Carbon $start, Carbon $end): array + { + /** @var GroupCollectorInterface $collector */ + $collector = app(GroupCollectorInterface::class); + + $collector->setUser($this->user); + $collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget(); + + if ($accounts->count() > 0) { + $collector->setAccounts($accounts); + } + $journals = $collector->getExtractedJournals(); + $return = []; + $total = []; + $currencies = []; + /** @var array $journal */ + foreach ($journals as $journal) { + $code = $journal['currency_code']; + if (!isset($currencies[$code])) { + $currencies[$code] = [ + 'id' => $journal['currency_id'], + 'name' => $journal['currency_name'], + 'symbol' => $journal['currency_symbol'], + 'decimal_places' => $journal['currency_decimal_places'], + ]; + } + $total[$code] = isset($total[$code]) ? bcadd($total[$code], $journal['amount']) : $journal['amount']; + } + foreach ($total as $code => $spent) { + /** @var TransactionCurrency $currency */ + $currency = $currencies[$code]; + $return[] = [ + 'currency_id' => $currency['id'], + 'currency_code' => $code, + 'currency_name' => $currency['name'], + 'currency_symbol' => $currency['symbol'], + 'currency_decimal_places' => $currency['decimal_places'], + 'amount' => $spent, + ]; + } + + return $return; + } } \ No newline at end of file diff --git a/app/Repositories/Budget/NoBudgetRepositoryInterface.php b/app/Repositories/Budget/NoBudgetRepositoryInterface.php index 39a07c1b35..0fec42f2df 100644 --- a/app/Repositories/Budget/NoBudgetRepositoryInterface.php +++ b/app/Repositories/Budget/NoBudgetRepositoryInterface.php @@ -33,21 +33,21 @@ use Illuminate\Support\Collection; */ interface NoBudgetRepositoryInterface { + /** + * @param Collection $accounts + * @param Carbon $start + * @param Carbon $end + * + * @return array + * @deprecated + */ + public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array; + /** * @param User $user */ public function setUser(User $user): void; - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return array - * @deprecated - */ - public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array; - /** * @param Collection $accounts * @param Carbon $start