mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 02:45:58 +00:00
Start refactoring budget repositories.
This commit is contained in:
@@ -22,8 +22,16 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Providers;
|
namespace FireflyIII\Providers;
|
||||||
|
|
||||||
|
use FireflyIII\Repositories\Budget\AvailableBudgetRepository;
|
||||||
|
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Budget\BudgetLimitRepository;
|
||||||
|
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepository;
|
use FireflyIII\Repositories\Budget\BudgetRepository;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Budget\NoBudgetRepository;
|
||||||
|
use FireflyIII\Repositories\Budget\NoBudgetRepositoryInterface;
|
||||||
|
use FireflyIII\Repositories\Budget\OperationsRepository;
|
||||||
|
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
@@ -47,7 +55,7 @@ class BudgetServiceProvider extends ServiceProvider
|
|||||||
{
|
{
|
||||||
$this->app->bind(
|
$this->app->bind(
|
||||||
BudgetRepositoryInterface::class,
|
BudgetRepositoryInterface::class,
|
||||||
function (Application $app) {
|
static function (Application $app) {
|
||||||
/** @var BudgetRepositoryInterface $repository */
|
/** @var BudgetRepositoryInterface $repository */
|
||||||
$repository = app(BudgetRepository::class);
|
$repository = app(BudgetRepository::class);
|
||||||
if ($app->auth->check()) {
|
if ($app->auth->check()) {
|
||||||
@@ -57,5 +65,62 @@ class BudgetServiceProvider extends ServiceProvider
|
|||||||
return $repository;
|
return $repository;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// available budget repos
|
||||||
|
$this->app->bind(
|
||||||
|
AvailableBudgetRepositoryInterface::class,
|
||||||
|
static function (Application $app) {
|
||||||
|
/** @var AvailableBudgetRepositoryInterface $repository */
|
||||||
|
$repository = app(AvailableBudgetRepository::class);
|
||||||
|
if ($app->auth->check()) {
|
||||||
|
$repository->setUser(auth()->user());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $repository;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// budget limit repository.
|
||||||
|
$this->app->bind(
|
||||||
|
BudgetLimitRepositoryInterface::class,
|
||||||
|
static function (Application $app) {
|
||||||
|
/** @var BudgetLimitRepositoryInterface $repository */
|
||||||
|
$repository = app(BudgetLimitRepository::class);
|
||||||
|
if ($app->auth->check()) {
|
||||||
|
$repository->setUser(auth()->user());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $repository;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// no budget repos
|
||||||
|
$this->app->bind(
|
||||||
|
NoBudgetRepositoryInterface::class,
|
||||||
|
static function (Application $app) {
|
||||||
|
/** @var NoBudgetRepositoryInterface $repository */
|
||||||
|
$repository = app(NoBudgetRepository::class);
|
||||||
|
if ($app->auth->check()) {
|
||||||
|
$repository->setUser(auth()->user());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $repository;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// operations repos
|
||||||
|
$this->app->bind(
|
||||||
|
OperationsRepositoryInterface::class,
|
||||||
|
static function (Application $app) {
|
||||||
|
/** @var OperationsRepositoryInterface $repository */
|
||||||
|
$repository = app(OperationsRepository::class);
|
||||||
|
if ($app->auth->check()) {
|
||||||
|
$repository->setUser(auth()->user());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $repository;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
57
app/Repositories/Budget/AvailableBudgetRepository.php
Normal file
57
app/Repositories/Budget/AvailableBudgetRepository.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
use FireflyIII\User;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* AvailableBudgetRepositoryInterface.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;
|
||||||
|
|
||||||
|
use FireflyIII\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface AvailableBudgetRepositoryInterface
|
||||||
|
*/
|
||||||
|
interface AvailableBudgetRepositoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void;
|
||||||
|
}
|
57
app/Repositories/Budget/BudgetLimitRepository.php
Normal file
57
app/Repositories/Budget/BudgetLimitRepository.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* BudgetLimitRepository.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;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Class BudgetLimitRepository
|
||||||
|
*/
|
||||||
|
class BudgetLimitRepository implements BudgetLimitRepositoryInterface
|
||||||
|
{
|
||||||
|
/** @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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
}
|
37
app/Repositories/Budget/BudgetLimitRepositoryInterface.php
Normal file
37
app/Repositories/Budget/BudgetLimitRepositoryInterface.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* BudgetLimitRepositoryInterface.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;
|
||||||
|
|
||||||
|
use FireflyIII\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface BudgetLimitRepositoryInterface
|
||||||
|
*/
|
||||||
|
interface BudgetLimitRepositoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void;
|
||||||
|
}
|
@@ -62,37 +62,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A method that returns the amount of money budgeted per day for this budget,
|
|
||||||
* on average.
|
|
||||||
*
|
|
||||||
* @param Budget $budget
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function budgetedPerDay(Budget $budget): string
|
|
||||||
{
|
|
||||||
Log::debug(sprintf('Now with budget #%d "%s"', $budget->id, $budget->name));
|
|
||||||
$total = '0';
|
|
||||||
$count = 0;
|
|
||||||
foreach ($budget->budgetlimits as $limit) {
|
|
||||||
$diff = $limit->start_date->diffInDays($limit->end_date);
|
|
||||||
$diff = 0 === $diff ? 1 : $diff;
|
|
||||||
$amount = (string)$limit->amount;
|
|
||||||
$perDay = bcdiv($amount, (string)$diff);
|
|
||||||
$total = bcadd($total, $perDay);
|
|
||||||
$count++;
|
|
||||||
Log::debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total));
|
|
||||||
}
|
|
||||||
$avg = $total;
|
|
||||||
if ($count > 0) {
|
|
||||||
$avg = bcdiv($total, (string)$count);
|
|
||||||
}
|
|
||||||
Log::debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
|
|
||||||
|
|
||||||
return $avg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
* // it's 5.
|
* // it's 5.
|
||||||
|
@@ -36,16 +36,6 @@ use Illuminate\Support\Collection;
|
|||||||
interface BudgetRepositoryInterface
|
interface BudgetRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* A method that returns the amount of money budgeted per day for this budget,
|
|
||||||
* on average.
|
|
||||||
*
|
|
||||||
* @param Budget $budget
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function budgetedPerDay(Budget $budget): string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@@ -156,26 +146,28 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array;
|
public function getAvailableBudgetWithCurrency(Carbon $start, Carbon $end): array;
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns all available budget objects.
|
|
||||||
*
|
|
||||||
* @param Carbon|null $start
|
|
||||||
* @param Carbon|null $end
|
|
||||||
* @return Collection
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO only used in API
|
* TODO only used in API
|
||||||
*
|
*
|
||||||
* Returns all available budget objects.
|
* Returns all available budget objects.
|
||||||
*
|
*
|
||||||
* @param TransactionCurrency $currency
|
* @param TransactionCurrency $currency
|
||||||
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection;
|
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all available budget objects.
|
||||||
|
*
|
||||||
|
* @param Carbon|null $start
|
||||||
|
* @param Carbon|null $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function getAvailableBudgetsByDate(?Carbon $start, ?Carbon $end): Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
@@ -210,7 +202,6 @@ interface BudgetRepositoryInterface
|
|||||||
public function getByIds(array $budgetIds): Collection;
|
public function getByIds(array $budgetIds): Collection;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
@@ -249,7 +240,6 @@ interface BudgetRepositoryInterface
|
|||||||
public function setBudgetOrder(Budget $budget, int $order): void;
|
public function setBudgetOrder(Budget $budget, int $order): void;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param User $user
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
@@ -257,6 +247,7 @@ interface BudgetRepositoryInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO this method is not multi-currency aware.
|
* TODO this method is not multi-currency aware.
|
||||||
|
*
|
||||||
* @param Collection $budgets
|
* @param Collection $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
@@ -267,7 +258,6 @@ interface BudgetRepositoryInterface
|
|||||||
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;
|
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return multi-currency spent information.
|
* Return multi-currency spent information.
|
||||||
*
|
*
|
||||||
|
58
app/Repositories/Budget/NoBudgetRepository.php
Normal file
58
app/Repositories/Budget/NoBudgetRepository.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* NoBudgetRepository.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;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Class NoBudgetRepository
|
||||||
|
*/
|
||||||
|
class NoBudgetRepository implements NoBudgetRepositoryInterface
|
||||||
|
{
|
||||||
|
/** @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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
}
|
38
app/Repositories/Budget/NoBudgetRepositoryInterface.php
Normal file
38
app/Repositories/Budget/NoBudgetRepositoryInterface.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* NoBudgetRepositoryInterface.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;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyIII\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface NoBudgetRepositoryInterface
|
||||||
|
*/
|
||||||
|
interface NoBudgetRepositoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void;
|
||||||
|
}
|
88
app/Repositories/Budget/OperationsRepository.php
Normal file
88
app/Repositories/Budget/OperationsRepository.php
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* OperationsRepository.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;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Budget;
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Class OperationsRepository
|
||||||
|
*/
|
||||||
|
class OperationsRepository implements OperationsRepositoryInterface
|
||||||
|
{
|
||||||
|
/** @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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method that returns the amount of money budgeted per day for this budget,
|
||||||
|
* on average.
|
||||||
|
*
|
||||||
|
* @param Budget $budget
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function budgetedPerDay(Budget $budget): string
|
||||||
|
{
|
||||||
|
Log::debug(sprintf('Now with budget #%d "%s"', $budget->id, $budget->name));
|
||||||
|
$total = '0';
|
||||||
|
$count = 0;
|
||||||
|
foreach ($budget->budgetlimits as $limit) {
|
||||||
|
$diff = $limit->start_date->diffInDays($limit->end_date);
|
||||||
|
$diff = 0 === $diff ? 1 : $diff;
|
||||||
|
$amount = (string)$limit->amount;
|
||||||
|
$perDay = bcdiv($amount, (string)$diff);
|
||||||
|
$total = bcadd($total, $perDay);
|
||||||
|
$count++;
|
||||||
|
Log::debug(sprintf('Found %d budget limits. Per day is %s, total is %s', $count, $perDay, $total));
|
||||||
|
}
|
||||||
|
$avg = $total;
|
||||||
|
if ($count > 0) {
|
||||||
|
$avg = bcdiv($total, (string)$count);
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('%s / %d = %s = average.', $total, $count, $avg));
|
||||||
|
|
||||||
|
return $avg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
}
|
49
app/Repositories/Budget/OperationsRepositoryInterface.php
Normal file
49
app/Repositories/Budget/OperationsRepositoryInterface.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* OperationsRepositoryInterface.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;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Budget;
|
||||||
|
use FireflyIII\User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface OperationsRepositoryInterface
|
||||||
|
*/
|
||||||
|
interface OperationsRepositoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function setUser(User $user): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method that returns the amount of money budgeted per day for this budget,
|
||||||
|
* on average.
|
||||||
|
*
|
||||||
|
* @param Budget $budget
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
|
public function budgetedPerDay(Budget $budget): string;
|
||||||
|
}
|
Reference in New Issue
Block a user