diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 6fe2ba4f8c..c003953b4c 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -20,6 +20,7 @@ use FireflyIII\Http\Requests\AccountFormRequest; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; +use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; @@ -196,12 +197,14 @@ class AccountController extends Controller } /** - * @param ARI $repository - * @param Account $account + * @param AccountTaskerInterface $tasker + * @param AccountCrudInterface $crud + * @param ARI $repository + * @param Account $account * * @return View */ - public function show(ARI $repository, Account $account) + public function show(AccountTaskerInterface $tasker, AccountCrudInterface $crud, ARI $repository, Account $account) { // show journals from current period only: $subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type); @@ -240,16 +243,15 @@ class AccountController extends Controller if ($cache->has()) { - $entries = $cache->get(); - - return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle')); + // $entries = $cache->get(); + // return view('accounts.show', compact('account', 'what', 'entries', 'subTitleIcon', 'journals', 'subTitle')); } - + $assets = $crud->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); while ($end >= $start) { $end = Navigation::startOfPeriod($end, $range); $currentEnd = Navigation::endOfPeriod($end, $range); - $spent = $this->spentInPeriod($account, $end, $currentEnd); - $earned = $this->earnedInPeriod($account, $end, $currentEnd); + $spent = $tasker->amountOutInPeriod(new Collection([$account]), $assets, $end, $currentEnd); + $earned = $tasker->amountInInPeriod(new Collection([$account]), $assets, $end, $currentEnd); $dateStr = $end->format('Y-m-d'); $dateName = Navigation::periodShow($end, $range); $entries->push([$dateStr, $dateName, $spent, $earned]); @@ -391,69 +393,4 @@ class AccountController extends Controller return ''; } - - /** - * Asset accounts actually earn money by being the destination of a deposit or the destination - * of a transfer. The money moves to them. - * - * A revenue account doesn't really earn money itself. Money is earned "from" the revenue account. - * So, the call to find out how many money has been earned by/from a revenue account is slightly different. - * - * - * - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - private function earnedInPeriod(Account $account, Carbon $start, Carbon $end) - { - /** @var ARI $repository */ - $repository = app(ARI::class); - $collection = new Collection([$account]); - $type = $account->accountType->type; - switch ($type) { - case AccountType::DEFAULT: - case AccountType::ASSET: - return $repository->earnedInPeriod($collection, $start, $end); - case AccountType::REVENUE: - return $repository->earnedFromInPeriod($collection, $start, $end); - default: - return '0'; - } - } - - /** - * Asset accounts actually spend money by being the source of a withdrawal or the source - * of a transfer. The money moves away from them. - * - * An expense account doesn't really spend money itself. Money is spent "at" the expense account. - * So, the call to find out how many money has been spent on/at an expense account is slightly different. - * - * - * - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - private function spentInPeriod(Account $account, Carbon $start, Carbon $end): string - { - /** @var ARI $repository */ - $repository = app(ARI::class); - $collection = new Collection([$account]); - $type = $account->accountType->type; - switch ($type) { - case AccountType::DEFAULT: - case AccountType::ASSET: - return $repository->spentInPeriod($collection, $start, $end); - case AccountType::EXPENSE: - return $repository->spentAtInPeriod($collection, $start, $end); - default: - return '0'; - } - } - } diff --git a/app/Http/Controllers/Admin/ConfigurationController.php b/app/Http/Controllers/Admin/ConfigurationController.php index 489b1d96e9..5b6c256622 100644 --- a/app/Http/Controllers/Admin/ConfigurationController.php +++ b/app/Http/Controllers/Admin/ConfigurationController.php @@ -60,6 +60,8 @@ class ConfigurationController extends Controller /** * @param ConfigurationRequest $request + * + * @return \Illuminate\Http\RedirectResponse */ public function store(ConfigurationRequest $request) { diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 1362584111..e805c6fc6c 100755 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -112,6 +112,8 @@ class LoginController extends Controller /** * Show the application login form. * + * @param Request $request + * * @return \Illuminate\Http\Response */ public function showLoginForm(Request $request) diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 35cd6a7ec0..bb87bd81e3 100755 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -117,6 +117,8 @@ class RegisterController extends Controller * OLD * Show the application registration form. * + * @param Request $request + * * @return \Illuminate\Http\Response */ public function showRegistrationForm(Request $request) diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 0e255ec0db..b93a52e8bb 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -139,6 +139,8 @@ class BudgetController extends Controller /** * Shows a budget list with spent/left/overspent. * + * @param BudgetRepositoryInterface $repository + * * @return \Symfony\Component\HttpFoundation\Response */ public function frontpage(BudgetRepositoryInterface $repository) diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 261c2fa39d..941d66da86 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -17,7 +17,7 @@ namespace FireflyIII\Http\Controllers\Chart; use Carbon\Carbon; use FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface; use FireflyIII\Http\Controllers\Controller; -use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; use Navigation; @@ -92,15 +92,16 @@ class ReportController extends Controller /** - * @param AccountRepositoryInterface $repository - * @param string $reportType - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts + * @param AccountTaskerInterface $accountTasker + * @param string $reportType + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse + * @internal param AccountRepositoryInterface $repository */ - public function yearInOut(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + public function yearInOut(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: $cache = new CacheProperties; @@ -120,8 +121,8 @@ class ReportController extends Controller while ($currentStart <= $end) { $currentEnd = Navigation::endOfPeriod($currentStart, '1M'); $date = $currentStart->format('Y-m'); - $spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd); - $earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd); + $spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd); + $earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd); $spentArray[$date] = bcmul($spent, '-1'); $earnedArray[$date] = $earned; $currentStart = Navigation::addPeriod($currentStart, '1M', 0); @@ -145,15 +146,16 @@ class ReportController extends Controller } /** - * @param AccountRepositoryInterface $repository - * @param string $reportType - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts + * @param AccountTaskerInterface $accountTasker + * @param string $reportType + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts * * @return \Illuminate\Http\JsonResponse + * @internal param AccountRepositoryInterface $repository */ - public function yearInOutSummarized(AccountRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + public function yearInOutSummarized(AccountTaskerInterface $accountTasker, string $reportType, Carbon $start, Carbon $end, Collection $accounts) { // chart properties for cache: @@ -174,8 +176,8 @@ class ReportController extends Controller while ($currentStart <= $end) { $currentEnd = Navigation::endOfPeriod($currentStart, '1M'); $date = $currentStart->format('Y-m'); - $spent = $repository->spentInPeriod($accounts, $currentStart, $currentEnd); - $earned = $repository->earnedInPeriod($accounts, $currentStart, $currentEnd); + $spent = $accountTasker->amountOutInPeriod($accounts, $accounts, $currentStart, $currentEnd); + $earned = $accountTasker->amountInInPeriod($accounts, $accounts, $currentStart, $currentEnd); $spentArray[$date] = bcmul($spent, '-1'); $earnedArray[$date] = $earned; $currentStart = Navigation::addPeriod($currentStart, '1M', 0); diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 9153196476..5857040e44 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -17,7 +17,7 @@ use Carbon\Carbon; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\AccountType; -use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; +use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Category\CategoryRepositoryInterface as CRI; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; @@ -97,12 +97,13 @@ class JsonController extends Controller } /** - * @param ARI $accountRepository - * @param AccountCrudInterface $crud + * @param AccountTaskerInterface $accountTasker + * @param AccountCrudInterface $crud * * @return \Illuminate\Http\JsonResponse + * @internal param ARI $accountRepository */ - public function boxIn(ARI $accountRepository, AccountCrudInterface $crud) + public function boxIn(AccountTaskerInterface $accountTasker, AccountCrudInterface $crud) { $start = session('start', Carbon::now()->startOfMonth()); $end = session('end', Carbon::now()->endOfMonth()); @@ -116,7 +117,8 @@ class JsonController extends Controller return Response::json($cache->get()); } $accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]); - $amount = $accountRepository->earnedInPeriod($accounts, $start, $end); + $assets = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); + $amount = $accountTasker->amountInInPeriod($accounts, $assets, $start, $end); $data = ['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; $cache->store($data); @@ -124,12 +126,13 @@ class JsonController extends Controller } /** - * @param ARI $accountRepository - * @param AccountCrudInterface $crud + * @param AccountCrudInterface $crud + * @param AccountTaskerInterface $accountTasker * * @return \Symfony\Component\HttpFoundation\Response + * @internal param ARI $accountRepository */ - public function boxOut(ARI $accountRepository, AccountCrudInterface $crud) + public function boxOut(AccountCrudInterface $crud, AccountTaskerInterface $accountTasker) { $start = session('start', Carbon::now()->startOfMonth()); $end = session('end', Carbon::now()->endOfMonth()); @@ -144,7 +147,8 @@ class JsonController extends Controller } $accounts = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]); - $amount = $accountRepository->spentInPeriod($accounts, $start, $end); + $assets = $crud->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); + $amount = $accountTasker->amountOutInPeriod($accounts, $assets, $start, $end); $data = ['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; $cache->store($data); diff --git a/app/Http/Controllers/NewUserController.php b/app/Http/Controllers/NewUserController.php index a59a2b38ba..974831a0e8 100644 --- a/app/Http/Controllers/NewUserController.php +++ b/app/Http/Controllers/NewUserController.php @@ -15,7 +15,6 @@ namespace FireflyIII\Http\Controllers; use Carbon\Carbon; use FireflyIII\Crud\Account\AccountCrudInterface; use FireflyIII\Http\Requests\NewUserFormRequest; -use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use Preferences; use Session; use View; diff --git a/app/Import/Importer/CsvImporter.php b/app/Import/Importer/CsvImporter.php index 21bc7813f7..797335cb1d 100644 --- a/app/Import/Importer/CsvImporter.php +++ b/app/Import/Importer/CsvImporter.php @@ -99,6 +99,7 @@ class CsvImporter implements ImporterInterface * @param array $row * * @return ImportEntry + * @throws FireflyException */ private function importSingleRow(int $index, array $row): ImportEntry { diff --git a/app/Import/Specifics/IngDescription.php b/app/Import/Specifics/IngDescription.php index 967b25b6d0..aa03ff06d1 100644 --- a/app/Import/Specifics/IngDescription.php +++ b/app/Import/Specifics/IngDescription.php @@ -63,7 +63,7 @@ class IngDescription implements SpecificInterface $this->removeIBANIngDescription(); $this->removeNameIngDescription(); break; - case 'BA' : // Betaalautomaat + case 'BA': // Betaalautomaat $this->addNameIngDescription(); break; } @@ -80,7 +80,7 @@ class IngDescription implements SpecificInterface */ protected function addNameIngDescription() { - $this->row[8] = $this->row[1] . " " . $this->row[8]; + $this->row[8] = $this->row[1] . ' ' . $this->row[8]; return true; } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index ea5b4377a8..73656e48cd 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -23,7 +23,6 @@ use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\User; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Steam; @@ -51,108 +50,6 @@ class AccountRepository implements AccountRepositoryInterface $this->user = $user; } - /** - * This method is almost the same as ::earnedInPeriod, but only works for revenue accounts - * instead of the implied asset accounts for ::earnedInPeriod. ::earnedInPeriod will tell you - * how much money was earned by the given asset accounts. This method will tell you how much money - * these given revenue accounts sent. Ie. how much money was made FROM these revenue accounts. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedFromInPeriod(Collection $accounts, Carbon $start, Carbon $end): string - { - $query = $this->user->transactionJournals()->expanded()->sortCorrectly() - ->transactionTypes([TransactionType::DEPOSIT]); - - if ($end >= $start) { - $query->before($end)->after($start); - } - - if ($accounts->count() > 0) { - $accountIds = $accounts->pluck('id')->toArray(); - $query->leftJoin( - 'transactions as source', function (JoinClause $join) { - $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0); - } - ); - $query->whereIn('source.account_id', $accountIds); - $query->whereNull('source.deleted_at'); - - } - // remove group by - $query->getQuery()->getQuery()->groups = null; - - // get id's - $ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray(); - - // that should do it: - $sum = $this->user->transactions() - ->whereIn('transaction_journal_id', $ids) - ->where('amount', '>', '0') - ->whereNull('transactions.deleted_at') - ->sum('amount'); - - return strval($sum); - - } - - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string - { - $query = $this->user->transactionJournals()->expanded()->sortCorrectly() - ->transactionTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER]); - - if ($end >= $start) { - $query->before($end)->after($start); - } - - if ($accounts->count() > 0) { - $accountIds = $accounts->pluck('id')->toArray(); - $query->leftJoin( - 'transactions as destination', function (JoinClause $join) { - $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0); - } - ); - $query->leftJoin( - 'transactions as source', function (JoinClause $join) { - $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0); - } - ); - - $query->whereIn('destination.account_id', $accountIds); - $query->whereNotIn('source.account_id', $accountIds); - $query->whereNull('destination.deleted_at'); - $query->whereNull('source.deleted_at'); - - } - // remove group by - $query->getQuery()->getQuery()->groups = null; - - // get id's - $ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray(); - - - // that should do it: - $sum = $this->user->transactions() - ->whereIn('transaction_journal_id', $ids) - ->where('amount', '>', '0') - ->whereNull('transactions.deleted_at') - ->sum('amount'); - - return strval($sum); - - } - /** * This method will call AccountRepositoryInterface::journalsInPeriod and get all withdrawaks made from the given $accounts, * as well as the transfers that move away from those $accounts. This is a slightly sharper selection @@ -514,98 +411,4 @@ class AccountRepository implements AccountRepositoryInterface return $journal; } - - /** - * This method is almost the same as ::spentInPeriod, but only works for expense accounts - * instead of the implied asset accounts for ::spentInPeriod. ::spentInPeriod will tell you - * how much money was spent by the given asset accounts. This method will tell you how much money - * these given expense accounts received. Ie. how much money was spent AT these expense accounts. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentAtInPeriod(Collection $accounts, Carbon $start, Carbon $end): string - { - /** @var HasMany $query */ - $query = $this->user->transactionJournals()->expanded() - ->transactionTypes([TransactionType::WITHDRAWAL]); - if ($end >= $start) { - $query->before($end)->after($start); - } - - if ($accounts->count() > 0) { - $accountIds = $accounts->pluck('id')->toArray(); - $query->leftJoin( - 'transactions as destination', function (JoinClause $join) { - $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0); - } - ); - $query->whereIn('destination.account_id', $accountIds); - - } - // remove group by - $query->getQuery()->getQuery()->groups = null; - - // that should do it: - $sum = strval($query->sum('destination.amount')); - if (is_null($sum)) { - $sum = '0'; - } - $sum = bcmul($sum, '-1'); - - return $sum; - } - - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string - { - /** @var HasMany $query */ - $query = $this->user->transactionJournals()->expanded() - ->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER]); - if ($end >= $start) { - $query->before($end)->after($start); - } - - if ($accounts->count() > 0) { - $accountIds = $accounts->pluck('id')->toArray(); - $query->leftJoin( - 'transactions as source', function (JoinClause $join) { - $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0); - } - ); - - $query->leftJoin( - 'transactions as destination', function (JoinClause $join) { - $join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0); - } - ); - $query->whereIn('source.account_id', $accountIds); - $query->whereNotIn('destination.account_id', $accountIds); - $query->whereNull('source.deleted_at'); - $query->whereNull('destination.deleted_at'); - $query->distinct(); - - } - // remove group by - $query->getQuery()->getQuery()->groups = null; - $ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray(); - - $sum = $this->user->transactions() - ->whereIn('transaction_journal_id', $ids) - ->where('amount', '<', '0') - ->whereNull('transactions.deleted_at') - ->sum('amount'); - - return strval($sum); - } - } diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php index f4c628972b..5441d262d0 100644 --- a/app/Repositories/Account/AccountRepositoryInterface.php +++ b/app/Repositories/Account/AccountRepositoryInterface.php @@ -41,15 +41,6 @@ interface AccountRepositoryInterface */ public function earnedFromInPeriod(Collection $accounts, Carbon $start, Carbon $end): string; - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function earnedInPeriod(Collection $accounts, Carbon $start, Carbon $end): string; - /** * This method will call AccountRepositoryInterface::journalsInPeriod and get all withdrawaks made from the given $accounts, * as well as the transfers that move away from those $accounts. This is a slightly sharper selection @@ -160,27 +151,4 @@ interface AccountRepositoryInterface */ public function openingBalanceTransaction(Account $account) : TransactionJournal; - /** - * This method is almost the same as ::spentInPeriod, but only works for expense accounts - * instead of the implied asset accounts for ::spentInPeriod. ::spentInPeriod will tell you - * how much money was spent by the given asset accounts. This method will tell you how much money - * these given expense accounts received. Ie. how much money was spent AT these expense accounts. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentAtInPeriod(Collection $accounts, Carbon $start, Carbon $end): string; - - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function spentInPeriod(Collection $accounts, Carbon $start, Carbon $end): string; - } diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php index 5beb77c121..f9933ed89b 100644 --- a/app/Repositories/Account/AccountTasker.php +++ b/app/Repositories/Account/AccountTasker.php @@ -14,10 +14,12 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Account; use Carbon\Carbon; -use FireflyIII\Models\Account; -use FireflyIII\User; -use Illuminate\Support\Collection; use FireflyIII\Helpers\Collection\Account as AccountCollection; +use FireflyIII\Models\Account; +use FireflyIII\Models\Transaction; +use FireflyIII\User; +use Illuminate\Database\Query\JoinClause; +use Illuminate\Support\Collection; use Log; use Steam; @@ -26,7 +28,7 @@ use Steam; * * @package FireflyIII\Repositories\Account */ -class AccountTasker +class AccountTasker implements AccountTaskerInterface { /** @var User */ private $user; @@ -41,6 +43,64 @@ class AccountTasker $this->user = $user; } + /** + * @see self::amountInPeriod + * + * @param Collection $accounts + * @param Collection $excluded + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function amountInInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string + { + $idList = [ + 'accounts' => $accounts->pluck('id')->toArray(), + 'exclude' => $excluded->pluck('id')->toArray(), + ]; + + Log::debug( + 'Now calling amountInInPeriod.', + ['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'], + 'start' => $start->format('Y-m-d'), + 'end' => $end->format('Y-m-d'), + ] + ); + + return $this->amountInPeriod($idList, $start, $end, true); + + } + + /** + * @see self::amountInPeriod + * + * @param Collection $accounts + * @param Collection $excluded + * @param Carbon $start + * @param Carbon $end + * + * @return string + */ + public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string + { + $idList = [ + 'accounts' => $accounts->pluck('id')->toArray(), + 'exclude' => $excluded->pluck('id')->toArray(), + ]; + + Log::debug( + 'Now calling amountOutInPeriod.', + ['accounts' => $idList['accounts'], 'excluded' => $idList['exclude'], + 'start' => $start->format('Y-m-d'), + 'end' => $end->format('Y-m-d'), + ] + ); + + return $this->amountInPeriod($idList, $start, $end, false); + + } + /** * @param Carbon $start * @param Carbon $end @@ -96,4 +156,62 @@ class AccountTasker return $object; } + + /** + * Will return how much money has been going out (ie. spent) by the given account(s). + * Alternatively, will return how much money has been coming in (ie. earned) by the given accounts. + * + * Enter $incoming=true for any money coming in (income) + * Enter $incoming=false for any money going out (expenses) + * + * This means any money going out or in. You can also submit accounts to exclude, + * so transfers between accounts are not included. + * + * As a general rule: + * + * - Asset accounts should return both expenses and earnings. But could return 0. + * - Expense accounts (where money is spent) should only return earnings (the account gets money). + * - Revenue accounts (where money comes from) should only return expenses (they spend money). + * + * @param array $accounts + * @param Carbon $start + * @param Carbon $end + * @param bool $incoming + * + * @return string + */ + protected function amountInPeriod(array $accounts, Carbon $start, Carbon $end, bool $incoming): string + { + $joinModifier = $incoming ? '<' : '>'; + $selection = $incoming ? '>' : '<'; + + $query = Transaction + ::distinct() + ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + ->leftJoin( + 'transactions as other_side', function (JoinClause $join) use ($joinModifier) { + $join->on('transaction_journals.id', '=', 'other_side.transaction_journal_id')->where('other_side.amount', $joinModifier, 0); + } + ) + ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + ->where('transaction_journals.user_id', $this->user->id) + ->whereNull('transactions.deleted_at') + ->whereNull('transaction_journals.deleted_at') + ->whereIn('transactions.account_id', $accounts['accounts']) + ->where('transactions.amount', $selection, 0); + if (count($accounts['exclude']) > 0) { + $query->whereNotIn('other_side.account_id', $accounts['exclude']); + } + + $result = $query->get(['transactions.id', 'transactions.amount']); + $sum = strval($result->sum('amount')); + if (strlen($sum) === 0) { + Log::debug('Sum is empty.'); + $sum = '0'; + } + Log::debug(sprintf('Result is %s', $sum)); + + return $sum; + } } \ No newline at end of file diff --git a/app/Repositories/Account/AccountTaskerInterface.php b/app/Repositories/Account/AccountTaskerInterface.php index 73be6cb23b..52dcf95425 100644 --- a/app/Repositories/Account/AccountTaskerInterface.php +++ b/app/Repositories/Account/AccountTaskerInterface.php @@ -25,6 +25,30 @@ use Illuminate\Support\Collection; interface AccountTaskerInterface { + /** + * @param Collection $accounts + * @param Collection $excluded + * @param Carbon $start + * @param Carbon $end + * + * @see AccountTasker::amountInPeriod() + * + * @return string + */ + public function amountInInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string; + + /** + * @param Collection $accounts + * @param Collection $excluded + * @param Carbon $start + * @param Carbon $end + * + * @see AccountTasker::amountInPeriod() + * + * @return string + */ + public function amountOutInPeriod(Collection $accounts, Collection $excluded, Carbon $start, Carbon $end): string; + /** * @param Carbon $start * @param Carbon $end diff --git a/app/Repositories/ImportJob/ImportJobRepository.php b/app/Repositories/ImportJob/ImportJobRepository.php index e8683a611f..d7f4ac30cf 100644 --- a/app/Repositories/ImportJob/ImportJobRepository.php +++ b/app/Repositories/ImportJob/ImportJobRepository.php @@ -42,6 +42,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface * @param string $fileType * * @return ImportJob + * @throws FireflyException */ public function create(string $fileType): ImportJob { diff --git a/app/Support/FireflyConfig.php b/app/Support/FireflyConfig.php index a6a528b8a0..32ff7958c3 100644 --- a/app/Support/FireflyConfig.php +++ b/app/Support/FireflyConfig.php @@ -15,7 +15,6 @@ namespace FireflyIII\Support; use Cache; use FireflyIII\Models\Configuration; -use FireflyIII\Models\Preference; use Log; /** diff --git a/routes/console.php b/routes/console.php index a70b3bb62b..6cfd0b721d 100755 --- a/routes/console.php +++ b/routes/console.php @@ -8,8 +8,6 @@ */ declare(strict_types = 1); -use Illuminate\Foundation\Inspiring; - /* |-------------------------------------------------------------------------- | Console Routes @@ -20,7 +18,3 @@ use Illuminate\Foundation\Inspiring; | simple approach to interacting with each command's IO methods. | */ - -Artisan::command('inspire', function () { - $this->comment(Inspiring::quote()); -}); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 2f2d20ff72..451bd48bed 100755 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -4,6 +4,9 @@ use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; +/** + * Class ExampleTest + */ class ExampleTest extends TestCase { /** diff --git a/tests/TestCase.php b/tests/TestCase.php index 8208edcaf6..2781681df5 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,5 +1,8 @@