diff --git a/app/Providers/BudgetServiceProvider.php b/app/Providers/BudgetServiceProvider.php index 0046e2745b..7e7f4124dc 100644 --- a/app/Providers/BudgetServiceProvider.php +++ b/app/Providers/BudgetServiceProvider.php @@ -22,8 +22,16 @@ declare(strict_types=1); 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\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\Support\ServiceProvider; @@ -47,7 +55,7 @@ class BudgetServiceProvider extends ServiceProvider { $this->app->bind( BudgetRepositoryInterface::class, - function (Application $app) { + static function (Application $app) { /** @var BudgetRepositoryInterface $repository */ $repository = app(BudgetRepository::class); if ($app->auth->check()) { @@ -57,5 +65,62 @@ class BudgetServiceProvider extends ServiceProvider 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; + } + ); + } } diff --git a/app/Repositories/Budget/AvailableBudgetRepository.php b/app/Repositories/Budget/AvailableBudgetRepository.php new file mode 100644 index 0000000000..7f83c3cf73 --- /dev/null +++ b/app/Repositories/Budget/AvailableBudgetRepository.php @@ -0,0 +1,57 @@ +. + */ + +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; + } + +} \ No newline at end of file diff --git a/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php new file mode 100644 index 0000000000..5ddf8bd505 --- /dev/null +++ b/app/Repositories/Budget/AvailableBudgetRepositoryInterface.php @@ -0,0 +1,37 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Repositories\Budget; + +use FireflyIII\User; + +/** + * Interface AvailableBudgetRepositoryInterface + */ +interface AvailableBudgetRepositoryInterface +{ + /** + * @param User $user + */ + public function setUser(User $user): void; +} \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetLimitRepository.php b/app/Repositories/Budget/BudgetLimitRepository.php new file mode 100644 index 0000000000..713529a571 --- /dev/null +++ b/app/Repositories/Budget/BudgetLimitRepository.php @@ -0,0 +1,57 @@ +. + */ + +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; + } +} \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetLimitRepositoryInterface.php b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php new file mode 100644 index 0000000000..ecd9f6a6dc --- /dev/null +++ b/app/Repositories/Budget/BudgetLimitRepositoryInterface.php @@ -0,0 +1,37 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Repositories\Budget; + +use FireflyIII\User; + +/** + * Interface BudgetLimitRepositoryInterface + */ +interface BudgetLimitRepositoryInterface +{ + /** + * @param User $user + */ + public function setUser(User $user): void; +} \ No newline at end of file diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index c314db8ced..69c0cd22a1 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -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 * // it's 5. diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index d906d4af10..535e766a9f 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -36,16 +36,6 @@ use Illuminate\Support\Collection; 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 */ @@ -55,8 +45,8 @@ interface BudgetRepositoryInterface * This method collects various info on budgets, used on the budget page and on the index. * * @param Collection $budgets - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -82,7 +72,7 @@ interface BudgetRepositoryInterface public function destroyBudgetLimit(BudgetLimit $budgetLimit): void; /** - * @param int|null $budgetId + * @param int|null $budgetId * @param string|null $budgetName * * @return Budget|null @@ -130,8 +120,8 @@ interface BudgetRepositoryInterface /** * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return Collection */ @@ -139,8 +129,8 @@ interface BudgetRepositoryInterface /** * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return string */ @@ -156,26 +146,28 @@ interface BudgetRepositoryInterface */ 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 * * Returns all available budget objects. * * @param TransactionCurrency $currency + * * @return 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 Carbon $start @@ -188,8 +180,8 @@ interface BudgetRepositoryInterface /** * @param Collection $budgets * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -210,7 +202,6 @@ interface BudgetRepositoryInterface public function getByIds(array $budgetIds): Collection; - /** * @return Collection */ @@ -218,8 +209,8 @@ interface BudgetRepositoryInterface /** * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -234,9 +225,9 @@ interface BudgetRepositoryInterface /** * @param TransactionCurrency $currency - * @param Carbon $start - * @param Carbon $end - * @param string $amount + * @param Carbon $start + * @param Carbon $end + * @param string $amount * * @return AvailableBudget */ @@ -244,12 +235,11 @@ interface BudgetRepositoryInterface /** * @param Budget $budget - * @param int $order + * @param int $order */ public function setBudgetOrder(Budget $budget, int $order): void; - /** * @param User $user */ @@ -257,24 +247,24 @@ interface BudgetRepositoryInterface /** * TODO this method is not multi-currency aware. + * * @param Collection $budgets * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return string */ public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string; - /** * Return multi-currency spent information. * * @param Collection $budgets * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -282,8 +272,8 @@ interface BudgetRepositoryInterface /** * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return string */ @@ -291,8 +281,8 @@ interface BudgetRepositoryInterface /** * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end + * @param Carbon $start + * @param Carbon $end * * @return array */ @@ -315,7 +305,7 @@ interface BudgetRepositoryInterface /** * @param Budget $budget - * @param array $data + * @param array $data * * @return Budget */ @@ -323,7 +313,7 @@ interface BudgetRepositoryInterface /** * @param AvailableBudget $availableBudget - * @param array $data + * @param array $data * * @return AvailableBudget */ @@ -331,7 +321,7 @@ interface BudgetRepositoryInterface /** * @param BudgetLimit $budgetLimit - * @param array $data + * @param array $data * * @return BudgetLimit */ diff --git a/app/Repositories/Budget/NoBudgetRepository.php b/app/Repositories/Budget/NoBudgetRepository.php new file mode 100644 index 0000000000..aed1346123 --- /dev/null +++ b/app/Repositories/Budget/NoBudgetRepository.php @@ -0,0 +1,58 @@ +. + */ + +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; + } +} \ No newline at end of file diff --git a/app/Repositories/Budget/NoBudgetRepositoryInterface.php b/app/Repositories/Budget/NoBudgetRepositoryInterface.php new file mode 100644 index 0000000000..4856c83274 --- /dev/null +++ b/app/Repositories/Budget/NoBudgetRepositoryInterface.php @@ -0,0 +1,38 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Repositories\Budget; + + +use FireflyIII\User; + +/** + * Interface NoBudgetRepositoryInterface + */ +interface NoBudgetRepositoryInterface +{ + /** + * @param User $user + */ + public function setUser(User $user): void; +} \ No newline at end of file diff --git a/app/Repositories/Budget/OperationsRepository.php b/app/Repositories/Budget/OperationsRepository.php new file mode 100644 index 0000000000..82aa1e455e --- /dev/null +++ b/app/Repositories/Budget/OperationsRepository.php @@ -0,0 +1,88 @@ +. + */ + +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; + } +} \ No newline at end of file diff --git a/app/Repositories/Budget/OperationsRepositoryInterface.php b/app/Repositories/Budget/OperationsRepositoryInterface.php new file mode 100644 index 0000000000..35dbcf7bc9 --- /dev/null +++ b/app/Repositories/Budget/OperationsRepositoryInterface.php @@ -0,0 +1,49 @@ +. + */ + +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; +} \ No newline at end of file