2019-08-29 21:33:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AvailableBudgetRepository.php
|
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 Exception;
|
|
|
|
use FireflyIII\Models\AvailableBudget;
|
2019-08-30 08:00:52 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2019-08-29 21:33:12 +02:00
|
|
|
use FireflyIII\User;
|
2019-08-30 08:02:11 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2019-08-29 21:33:12 +02:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class AvailableBudgetRepository
|
|
|
|
*/
|
|
|
|
class AvailableBudgetRepository implements AvailableBudgetRepositoryInterface
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if ('testing' === config('app.env')) {
|
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
|
|
|
die(get_class($this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 07:45:48 +02:00
|
|
|
/**
|
|
|
|
* @param AvailableBudget $availableBudget
|
|
|
|
*/
|
|
|
|
public function destroyAvailableBudget(AvailableBudget $availableBudget): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$availableBudget->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error(sprintf('Could not delete available budget: %s', $e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:00:52 +02: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)
|
|
|
|
->where('start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('end_date', $end->format('Y-m-d 00:00:00'))->first();
|
|
|
|
if (null !== $availableBudget) {
|
|
|
|
$amount = (string)$availableBudget->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
$availableBudgets = $this->user->availableBudgets()
|
|
|
|
->where('start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('end_date', $end->format('Y-m-d 00:00:00'))->get();
|
|
|
|
/** @var AvailableBudget $availableBudget */
|
|
|
|
foreach ($availableBudgets as $availableBudget) {
|
|
|
|
$return[$availableBudget->transaction_currency_id] = $availableBudget->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2019-08-30 08:03:13 +02: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) {
|
|
|
|
$query->where('start_date', '>=', $start->format('Y-m-d H:i:s'));
|
|
|
|
}
|
|
|
|
if (null !== $end) {
|
|
|
|
$query->where('end_date', '<=', $end->format('Y-m-d H:i:s'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->get();
|
|
|
|
}
|
|
|
|
|
2019-08-29 21:33:12 +02:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2019-08-30 07:45:48 +02:00
|
|
|
|
2019-08-29 21:33:12 +02:00
|
|
|
}
|