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

318 lines
9.7 KiB
PHP
Raw Normal View History

2019-08-29 14:33:12 -05:00
<?php
/**
* AvailableBudgetRepository.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2019-08-29 14:33:12 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-08-29 14:33:12 -05: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 14:33:12 -05:00
*
* This program is distributed in the hope that it will be useful,
2019-08-29 14:33:12 -05: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 14:33:12 -05: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 14:33:12 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Repositories\Budget;
2019-08-30 01:00:52 -05:00
use Carbon\Carbon;
2019-08-30 00:45:48 -05:00
use Exception;
use FireflyIII\Models\AvailableBudget;
2019-08-30 01:00:52 -05:00
use FireflyIII\Models\TransactionCurrency;
2019-08-29 14:33:12 -05:00
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
2019-08-30 01:02:11 -05:00
use Illuminate\Support\Collection;
2019-08-29 14:33:12 -05:00
/**
*
* Class AvailableBudgetRepository
*/
class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
{
2020-09-18 09:14:17 -05:00
private User $user;
2019-08-29 14:33:12 -05:00
2021-03-11 23:20:01 -06:00
/**
* Delete all available budgets.
*/
public function destroyAll(): void
{
$this->user->availableBudgets()->delete();
}
2019-08-30 00:45:48 -05:00
/**
* @param AvailableBudget $availableBudget
*/
public function destroyAvailableBudget(AvailableBudget $availableBudget): void
{
try {
$availableBudget->delete();
2021-04-07 00:28:43 -05:00
} catch (Exception $e) { // @phpstan-ignore-line
// @ignoreException
2019-08-30 00:45:48 -05:00
}
}
/**
* Find existing AB.
*
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return AvailableBudget|null
*/
public function find(TransactionCurrency $currency, Carbon $start, Carbon $end): ?AvailableBudget
{
return $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
2020-09-23 23:09:26 -05:00
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))
->first();
}
/**
* @inheritDoc
*/
public function findById(int $id): ?AvailableBudget
{
return $this->user->availableBudgets->find($id);
}
/**
* Return a list of all available budgets (in all currencies) (for the selected period).
*
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*/
public function get(?Carbon $start = null, ?Carbon $end = null): Collection
{
$query = $this->user->availableBudgets()->with(['transactionCurrency']);
if (null !== $start && null !== $end) {
$query->where(
static function (Builder $q1) use ($start, $end) {
2020-09-23 23:09:26 -05:00
$q1->where('start_date', '=', $start->format('Y-m-d'));
$q1->where('end_date', '=', $end->format('Y-m-d'));
}
);
}
return $query->get(['available_budgets.*']);
}
2019-08-30 01:00:52 -05:00
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
{
$amount = '0';
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
2020-09-23 23:09:26 -05:00
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
2019-08-30 01:00:52 -05:00
if (null !== $availableBudget) {
2022-03-29 07:59:58 -05:00
$amount = (string) $availableBudget->amount;
2019-08-30 01:00:52 -05:00
}
return $amount;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
{
$return = [];
$availableBudgets = $this->user->availableBudgets()
2020-09-23 23:09:26 -05:00
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->get();
2019-08-30 01:00:52 -05:00
/** @var AvailableBudget $availableBudget */
foreach ($availableBudgets as $availableBudget) {
$return[$availableBudget->transaction_currency_id] = $availableBudget->amount;
}
return $return;
}
2019-08-30 01:03:13 -05:00
/**
* Returns all available budget objects.
*
* @param TransactionCurrency $currency
*
* @return Collection
*/
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection
{
return $this->user->availableBudgets()->where('transaction_currency_id', $currency->id)->get();
}
/**
* Returns all available budget objects.
*
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return Collection
*
*/
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection
{
$query = $this->user->availableBudgets();
if (null !== $start) {
2020-09-23 23:09:26 -05:00
$query->where('start_date', '>=', $start->format('Y-m-d'));
2019-08-30 01:03:13 -05:00
}
if (null !== $end) {
2020-09-23 23:09:26 -05:00
$query->where('end_date', '<=', $end->format('Y-m-d'));
2019-08-30 01:03:13 -05:00
}
return $query->get();
}
2021-03-11 23:20:01 -06:00
/**
* @inheritDoc
*/
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();
}
2019-08-30 01:19:55 -05:00
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return AvailableBudget
2019-09-04 10:39:39 -05:00
* @deprecated
2019-08-30 01:19:55 -05:00
*/
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): AvailableBudget
{
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
2020-09-23 23:09:26 -05:00
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
2019-08-30 01:19:55 -05:00
if (null === $availableBudget) {
$availableBudget = new AvailableBudget;
$availableBudget->user()->associate($this->user);
$availableBudget->transactionCurrency()->associate($currency);
2020-09-23 23:09:26 -05:00
$availableBudget->start_date = $start->format('Y-m-d');
$availableBudget->end_date = $end->format('Y-m-d');
2019-08-30 01:19:55 -05:00
}
$availableBudget->amount = $amount;
$availableBudget->save();
return $availableBudget;
}
2019-08-29 14:33:12 -05:00
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $data
*
* @return AvailableBudget|null
*/
public function store(array $data): ?AvailableBudget
{
2020-09-23 23:06:18 -05:00
$start = $data['start'];
2021-03-11 23:20:01 -06:00
if ($start instanceof Carbon) {
2020-09-23 23:06:18 -05:00
$start = $data['start']->startOfDay();
}
$end = $data['end'];
2021-03-11 23:20:01 -06:00
if ($end instanceof Carbon) {
2020-09-23 23:06:18 -05:00
$end = $data['end']->endOfDay();
}
2021-03-11 23:20:01 -06:00
return AvailableBudget::create(
[
'user_id' => $this->user->id,
2021-03-13 09:47:29 -06:00
'transaction_currency_id' => $data['currency_id'],
'amount' => $data['amount'],
2020-09-23 23:06:18 -05:00
'start_date' => $start,
'end_date' => $end,
]
);
}
/**
* @param AvailableBudget $availableBudget
* @param array $data
*
* @return AvailableBudget
*/
public function update(AvailableBudget $availableBudget, array $data): AvailableBudget
{
2021-04-07 00:28:43 -05:00
if (array_key_exists('amount', $data)) {
$availableBudget->amount = $data['amount'];
}
$availableBudget->save();
return $availableBudget;
}
2019-08-30 02:19:29 -05:00
/**
* @param AvailableBudget $availableBudget
* @param array $data
*
* @return AvailableBudget
*/
public function updateAvailableBudget(AvailableBudget $availableBudget, array $data): AvailableBudget
{
2021-03-13 09:47:29 -06:00
if (array_key_exists('start', $data)) {
$start = $data['start'];
if ($start instanceof Carbon) {
$start = $data['start']->startOfDay();
$availableBudget->start_date = $start;
$availableBudget->save();
}
2019-08-30 02:19:29 -05:00
}
2020-09-23 23:06:18 -05:00
2021-03-13 09:47:29 -06:00
if (array_key_exists('end', $data)) {
$end = $data['end'];
if ($end instanceof Carbon) {
$end = $data['end']->endOfDay();
$availableBudget->end_date = $end;
$availableBudget->save();
}
2020-09-23 23:06:18 -05:00
}
2021-03-13 09:47:29 -06: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-23 23:06:18 -05:00
}
2019-08-30 02:19:29 -05:00
return $availableBudget;
}
}