Files
firefly-iii/app/Repositories/Budget/AvailableBudgetRepository.php

304 lines
11 KiB
PHP
Raw Normal View History

2019-08-29 21:33:12 +02:00
<?php
2019-08-29 21:33:12 +02:00
/**
* AvailableBudgetRepository.php
2020-02-16 14:00:57 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2019-08-29 21:33:12 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-08-29 21:33:12 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-08-29 21:33:12 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-08-29 21:33:12 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-08-29 21:33:12 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-08-29 21:33:12 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\Budget;
2019-08-30 08:00:52 +02:00
use Carbon\Carbon;
2019-08-30 07:45:48 +02:00
use FireflyIII\Models\AvailableBudget;
2019-08-30 08:00:52 +02:00
use FireflyIII\Models\TransactionCurrency;
2024-12-28 07:35:20 +01:00
use FireflyIII\Support\Facades\Amount;
2019-08-29 21:33:12 +02:00
use FireflyIII\User;
2023-02-19 08:43:28 +01:00
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
2019-08-30 08:02:11 +02:00
use Illuminate\Support\Collection;
2024-01-05 10:55:46 +01:00
use Illuminate\Support\Facades\Log;
2019-08-29 21:33:12 +02:00
/**
* Class AvailableBudgetRepository
*/
class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
{
2020-09-18 16:14:17 +02:00
private User $user;
2019-08-29 21:33:12 +02:00
public function cleanup(): void
{
$exists = [];
$availableBudgets = $this->user->availableBudgets()->get();
2023-12-20 19:35:52 +01:00
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$start = $availableBudget->start_date->format('Y-m-d');
$end = $availableBudget->end_date->format('Y-m-d');
$key = sprintf('%s-%s-%s', $availableBudget->transaction_currency_id, $start, $end);
if (array_key_exists($key, $exists)) {
app('log')->debug(sprintf('Found duplicate AB: %s %s, %s-%s. Has been deleted', $availableBudget->transaction_currency_id, $availableBudget->amount, $start, $end));
$availableBudget->delete();
}
$exists[$key] = true;
}
}
/**
* Return a list of all available budgets (in all currencies) (for the selected period).
*/
public function get(?Carbon $start = null, ?Carbon $end = null): Collection
{
$query = $this->user->availableBudgets()->with(['transactionCurrency']);
if (null !== $start && null !== $end) {
$query->where(
2023-12-21 05:07:26 +01:00
static function (Builder $q1) use ($start, $end): void { // @phpstan-ignore-line
2024-12-30 10:51:34 +01:00
$q1->where('start_date', '=', $start->format('Y-m-d'));
$q1->where('end_date', '=', $end->format('Y-m-d'));
}
);
}
2024-12-30 10:51:34 +01:00
$result = $query->get(['available_budgets.*']);
2024-12-30 12:22:39 +01:00
Log::debug(sprintf('Found %d available budgets between %s and %s', $result->count(), $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
2024-12-30 10:51:34 +01:00
return $result;
}
2021-03-12 06:20:01 +01:00
/**
* Delete all available budgets.
*/
public function destroyAll(): void
{
2024-01-05 10:55:46 +01:00
Log::channel('audit')->info('Delete all available budgets through destroyAll');
2021-03-12 06:20:01 +01:00
$this->user->availableBudgets()->delete();
}
2019-08-30 07:45:48 +02:00
public function destroyAvailableBudget(AvailableBudget $availableBudget): void
{
2022-12-30 09:28:03 +01:00
$availableBudget->delete();
2019-08-30 07:45:48 +02:00
}
2023-05-29 13:56:55 +02:00
public function findById(int $id): ?AvailableBudget
{
return $this->user->availableBudgets->find($id);
}
/**
2023-06-21 12:34:58 +02:00
* Find existing AB.
2023-05-29 13:56:55 +02:00
*/
2023-06-21 12:34:58 +02:00
public function find(TransactionCurrency $currency, Carbon $start, Carbon $end): ?AvailableBudget
2023-05-29 13:56:55 +02:00
{
2023-06-21 12:34:58 +02:00
return $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))
->first()
;
2023-05-29 13:56:55 +02:00
}
2019-08-30 08:00:52 +02:00
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
{
$amount = '0';
2023-12-20 19:35:52 +01:00
/** @var null|AvailableBudget $availableBudget */
2019-08-30 08:00:52 +02:00
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first()
;
2019-08-30 08:00:52 +02:00
if (null !== $availableBudget) {
2023-11-05 19:41:37 +01:00
$amount = $availableBudget->amount;
2019-08-30 08:00:52 +02:00
}
return $amount;
}
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
{
Log::debug(sprintf('Now in %s(%s, %s)', __METHOD__, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
2019-08-30 08:00:52 +02:00
$return = [];
$availableBudgets = $this->user->availableBudgets()
->where('start_date', $start->format('Y-m-d H:i:s'))
->where('end_date', $end->format('Y-m-d H:i:s'))->get()
;
Log::debug(sprintf('Found %d available budgets', $availableBudgets->count()));
// use native amount if necessary?
2024-12-28 07:35:20 +01:00
$convertToNative = Amount::convertToNative($this->user);
$default = Amount::getDefaultCurrency();
2023-12-20 19:35:52 +01:00
2019-08-30 08:00:52 +02:00
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$currencyId = $convertToNative && $availableBudget->transaction_currency_id !== $default->id ? $default->id : $availableBudget->transaction_currency_id;
$field = $convertToNative && $availableBudget->transaction_currency_id !== $default->id ? 'native_amount' : 'amount';
$return[$currencyId] ??= '0';
$return[$currencyId] = bcadd($return[$currencyId], $availableBudget->{$field});
Log::debug(sprintf('Add #%d %s (%s) for a total of %s', $currencyId, $availableBudget->{$field}, $field, $return[$currencyId]));
2019-08-30 08:00:52 +02:00
}
2019-08-30 08:00:52 +02:00
return $return;
}
2019-08-30 08:03:13 +02:00
/**
* Returns all available budget objects.
*/
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection
{
return $this->user->availableBudgets()->where('transaction_currency_id', $currency->id)->get();
}
/**
* Returns all available budget objects.
*/
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection
{
$query = $this->user->availableBudgets();
if (null !== $start) {
2020-09-24 06:09:26 +02:00
$query->where('start_date', '>=', $start->format('Y-m-d'));
2019-08-30 08:03:13 +02:00
}
if (null !== $end) {
2020-09-24 06:09:26 +02:00
$query->where('end_date', '<=', $end->format('Y-m-d'));
2019-08-30 08:03:13 +02:00
}
return $query->get();
}
/**
* Returns all available budget objects.
*/
public function getAvailableBudgetsByExactDate(Carbon $start, Carbon $end): Collection
{
return $this->user->availableBudgets()
->where('start_date', '=', $start->format('Y-m-d'))
->where('end_date', '=', $end->format('Y-m-d'))
->get()
;
}
2021-03-12 06:20:01 +01:00
public function getByCurrencyDate(Carbon $start, Carbon $end, TransactionCurrency $currency): ?AvailableBudget
{
return $this->user
->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first()
;
2021-03-12 06:20:01 +01:00
}
2019-08-30 08:19:55 +02:00
/**
2019-09-04 17:39:39 +02:00
* @deprecated
2019-08-30 08:19:55 +02:00
*/
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget
{
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first()
;
2019-08-30 08:19:55 +02:00
if (null === $availableBudget) {
$availableBudget = new AvailableBudget();
2019-08-30 08:19:55 +02:00
$availableBudget->user()->associate($this->user);
$availableBudget->transactionCurrency()->associate($currency);
2024-12-30 12:22:39 +01:00
$availableBudget->start_date = $start->startOfDay();
2024-11-06 11:12:26 +01:00
$availableBudget->start_date_tz = $start->format('e');
2024-12-30 12:22:39 +01:00
$availableBudget->end_date = $end->endOfDay();
2024-11-06 11:12:26 +01:00
$availableBudget->end_date_tz = $end->format('e');
2019-08-30 08:19:55 +02:00
}
$availableBudget->amount = $amount;
$availableBudget->save();
return $availableBudget;
}
public function setUser(null|Authenticatable|User $user): void
2019-08-29 21:33:12 +02:00
{
2023-10-30 19:49:40 +01:00
if ($user instanceof User) {
2023-02-19 08:43:28 +01:00
$this->user = $user;
}
2019-08-29 21:33:12 +02:00
}
public function store(array $data): ?AvailableBudget
{
2020-09-24 06:06:18 +02:00
$start = $data['start'];
2021-03-12 06:20:01 +01:00
if ($start instanceof Carbon) {
2020-09-24 06:06:18 +02:00
$start = $data['start']->startOfDay();
}
$end = $data['end'];
2021-03-12 06:20:01 +01:00
if ($end instanceof Carbon) {
2020-09-24 06:06:18 +02:00
$end = $data['end']->endOfDay();
}
2021-03-12 06:20:01 +01:00
return AvailableBudget::create(
[
'user_id' => $this->user->id,
'user_group_id' => $this->user->user_group_id,
2021-03-13 16:47:29 +01:00
'transaction_currency_id' => $data['currency_id'],
'amount' => $data['amount'],
2024-12-30 12:22:39 +01:00
'start_date' => $start,
2024-11-06 11:57:12 +01:00
'start_date_tz' => $start->format('e'),
2024-12-30 12:22:39 +01:00
'end_date' => $end,
2024-11-06 11:57:12 +01:00
'end_date_tz' => $end->format('e'),
]
);
}
public function update(AvailableBudget $availableBudget, array $data): AvailableBudget
{
2021-04-07 07:28:43 +02:00
if (array_key_exists('amount', $data)) {
$availableBudget->amount = $data['amount'];
}
$availableBudget->save();
return $availableBudget;
}
2019-08-30 09:19:29 +02:00
public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget
{
2021-03-13 16:47:29 +01:00
if (array_key_exists('start', $data)) {
$start = $data['start'];
if ($start instanceof Carbon) {
2024-11-06 11:57:12 +01:00
$start = $data['start']->startOfDay();
2024-12-30 12:22:39 +01:00
$availableBudget->start_date = $start;
2024-11-06 11:12:26 +01:00
$availableBudget->start_date_tz = $start->format('e');
2021-03-13 16:47:29 +01:00
$availableBudget->save();
}
2019-08-30 09:19:29 +02:00
}
2020-09-24 06:06:18 +02:00
2021-03-13 16:47:29 +01:00
if (array_key_exists('end', $data)) {
$end = $data['end'];
if ($end instanceof Carbon) {
2024-11-06 11:57:12 +01:00
$end = $data['end']->endOfDay();
2024-12-30 12:22:39 +01:00
$availableBudget->end_date = $end;
2024-11-06 11:12:26 +01:00
$availableBudget->end_date_tz = $end->format('e');
2021-03-13 16:47:29 +01:00
$availableBudget->save();
}
2020-09-24 06:06:18 +02:00
}
2021-03-13 16:47:29 +01:00
if (array_key_exists('currency_id', $data)) {
$availableBudget->transaction_currency_id = $data['currency_id'];
$availableBudget->save();
}
if (array_key_exists('amount', $data)) {
$availableBudget->amount = $data['amount'];
$availableBudget->save();
2020-09-24 06:06:18 +02:00
}
2019-08-30 09:19:29 +02:00
return $availableBudget;
}
}